Menu Close

Find all repeated elements in the array.

Repeating Elements
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive)

Input : [8,8,7,5,6,2,1,7,5]

Output:
8
7
5
def printRepeating(arr, size):

    print("The repeating elements are: ")
    for i in range(0, size):

        if arr[abs(arr[i])] >= 0:
            arr[abs(arr[i])] = -arr[abs(arr[i])]
        else:
            print(abs(arr[i]))


arr = list(map(int,input("Elements : ").split(' '))) #or arr=[8,8,7,5,6,2,1,7,5]
arr_size = len(arr)

printRepeating(arr, arr_size)

Input_1:
Elements : 8 8 7 5 6 2 1 7 5

Output:
The repeating elements are:
8
7
5


Input_2:
Elements : 7 5 7 6 2 1 5 6

Output:
The repeating elements are:
7
5
6



Input_3:
Elements : 1 2 3 1 2 4 6
Output:
The repeating elements are:
1
2


Input_4:
Elements : 0 1 0 3 3 5
Output:
The repeating elements are:
3


Illustration of the Output:

Executed using python3 linux

More Q