Python
- key differences between List and Arrays
- Python collections ChainMap
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python collections
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions in Python
- key differences between List and Arrays
- Program: Calculate Pi in Python
- String Formatting in Python
- Python counters
- python tuples
- Python deque
- Python dictionary
- Python Lists
- python namedtuple
Find out the duplicate in an integer array of 1 to n numbers
In this algorithm problem, you are given an integer array containing numbers from 1 to n. The task is to find the duplicate number(s) present in the array. The solution should efficiently identify and return the duplicate number(s) to ensure the array contains only unique elements from the given range. By implementing this algorithm, you can effectively detect any duplicate occurrences and handle the array accordingly.
def find_duplicates(nums):
array_len=len(nums)
count = [0] * (array_len+1) # Initialize count array with 101 elements (0 to n)
for num in nums:
count[num] += 1 # Increment count for each number encountered
duplicates = []
for i in range(1, (array_len+1)):
if count[i] > 1:
duplicates.append(i)
return duplicates
usage
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 17, 20, 20]
result = find_duplicates(numbers)
print(result)
Output
[10, 17, 20]