Find duplicates in a given array when elements are not limited to a range | Function to find the Duplicates , if duplicate occurs 2 times or more than 2 times in array so , it will print duplicate value only once at output ; Initialize ifPresent as false ; ArrayList to store the output ; Checking if element is present in the ArrayList or not if present then break ; If element is not present in the ArrayList then add it to ArrayList and make ifPresent at true ; If duplicates is present then print ArrayList ; Driver Code
def findDuplicates ( arr , Len ) : NEW_LINE INDENT ifPresent = False NEW_LINE a1 = [ ] NEW_LINE for i in range ( Len - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , Len ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT if arr [ i ] in a1 : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT a1 . append ( arr [ i ] ) NEW_LINE ifPresent = True NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ifPresent ) : NEW_LINE INDENT print ( a1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ duplicates ▁ present ▁ in ▁ arrays " ) NEW_LINE DEDENT DEDENT arr = [ 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE findDuplicates ( arr , n ) NEW_LINE