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]