Difference between sum of odd 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
def findSum ( arr , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT sum_odd , sum_even = 0 , 0 NEW_LINE for itr in mp : NEW_LINE INDENT if ( mp [ itr ] % 2 != 0 ) : NEW_LINE INDENT sum_odd += ( itr ) * ( mp [ itr ] ) NEW_LINE DEDENT if ( mp [ itr ] % 2 == 0 ) : NEW_LINE INDENT sum_even += ( itr ) * ( mp [ itr ] ) NEW_LINE DEDENT DEDENT diff = sum_even - sum_odd NEW_LINE return diff NEW_LINE DEDENT arr = [ 1 , 5 , 5 , 2 , 4 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findSum ( arr , N ) ) NEW_LINE