Difference between sum of odd and even frequent elements in an Array | C # program to find absolute difference between the sum of all odd frequenct and even frequent elements in an array ; Function to find the sum of all even and odd frequent elements in an array ; Stores the frequency of array elements ; Traverse the array ; Update frequency of current element ; Stores sum of odd and even frequent elements ; Traverse the map ; If frequency is odd ; Add sum of all occurrences of current element to sum_odd ; If frequency is even ; Add sum of all occurrences of current element to sum_even ; Calculate difference between their sum ; Return diff ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findSum ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } int sum_odd = 0 , sum_even = 0 ; foreach ( KeyValuePair < int , int > itr in mp ) { if ( itr . Value % 2 != 0 ) sum_odd += ( itr . Key ) * ( itr . Value ) ; if ( itr . Value % 2 == 0 ) sum_even += ( itr . Key ) * ( itr . Value ) ; } int diff = sum_even - sum_odd ; return diff ; } static void Main ( ) { int [ ] arr = { 1 , 5 , 5 , 2 , 4 , 3 , 3 } ; int N = arr . Length ; Console . Write ( findSum ( arr , N ) ) ; } }