Find the Number Occurring Odd Number of Times | C # 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
using System ; using System . Collections . Generic ; public class OddOccurrence { static int getOddOccurrence ( int [ ] arr , int n ) { Dictionary < int , int > hmap = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hmap . ContainsKey ( arr [ i ] ) ) { int val = hmap [ arr [ i ] ] ; hmap . Remove ( arr [ i ] ) ; hmap . Add ( arr [ i ] , val + 1 ) ; } else hmap . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > entry in hmap ) { if ( entry . Value % 2 != 0 ) { return entry . Key ; } } 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 ; Console . WriteLine ( getOddOccurrence ( arr , n ) ) ; } }