Find duplicates in a given array when elements are not limited to a range | C ++ implementation of the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDuplicates ( int arr [ ] , int len ) { bool ifPresent = false ; vector < int > al ; for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { auto it = std :: find ( al . begin ( ) , al . end ( ) , arr [ i ] ) ; if ( it != al . end ( ) ) { break ; } else { al . push_back ( arr [ i ] ) ; ifPresent = true ; } } } } if ( ifPresent == true ) { cout << " [ " << al [ 0 ] << " , ▁ " ; for ( int i = 1 ; i < al . size ( ) - 1 ; i ++ ) { cout << al [ i ] << " , ▁ " ; } cout << al [ al . size ( ) - 1 ] << " ] " ; } else { cout << " No ▁ duplicates ▁ present ▁ in ▁ arrays " ; } } int main ( ) { int arr [ ] = { 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDuplicates ( arr , n ) ; return 0 ; }