text: Find the two numbers with odd occurrences in an unsorted array | C ++ Program to find the two odd occurring elements ; Prints two numbers that occur odd number of times . The function assumes that the array size is at least 2 and there are exactly two numbers occurring odd number of times . ; Create map and calculate frequency of array of * elements using array . ; Traverse through the map and check if its second element that is the frequency is odd or not . Then this is the odd occurring element . Its is clearly mentioned in problem that there are only two odd occurring elements so this will print those two elements . ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwoOdd ( int arr [ ] , int size ) { map < int , int > m ; for ( int i = 0 ; i < size ; i ++ ) { m [ arr [ i ] ] ++ ; } cout << " The ▁ two ▁ ODD ▁ elements ▁ are ▁ " ; for ( auto & x : m ) { if ( x . second % 2 != 0 ) cout << x . first << " , ▁ " ; } } int main ( ) { int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 3 , 1 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printTwoOdd ( arr , arr_size ) ; return 0 ; }