Find the Number Occurring Odd Number of Times | Java program to find the element occurring odd number of times ; function to find the element occurring odd number of times ; Putting all elements into the HashMap ; If array element is already present then increase the count of that element . ; if array element is not present then put element into the HashMap and initialize the count to one . ; Checking for odd occurrence of each element present in the HashMap ; driver code
import java . io . * ; import java . util . HashMap ; class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int n ) { HashMap < Integer , Integer > hmap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hmap . containsKey ( arr [ i ] ) ) { int val = hmap . get ( arr [ i ] ) ; hmap . put ( arr [ i ] , val + 1 ) ; } else hmap . put ( arr [ i ] , 1 ) ; } for ( Integer a : hmap . keySet ( ) ) { if ( hmap . get ( a ) % 2 != 0 ) return a ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }