Given an array, find the integer that appears an odd number of times. code example

Example 1: you are given an array of integers. your task is to print the sum of numbers that occurs for an even number of times in the array.

import collections
def fun(arr):
    mp = collections.defaultdict(int)
      
    for i in range(len(arr)):
        mp[arr[i]] += 1 
    sum = 0 
    for i in mp.keys(): 
          
        
        if (mp[i] % 2 == 0): 
            sum += i
    return sum
n= int(input())
arr = list(map(int,input().split()))
print(fun(arr))

Example 2: check if number appears odd number of times in array javascript

function findOdd(A) {
    let counts = A.reduce((p, n) => (p[n] = ++p[n] || 1, p), {});
    return +Object.keys(counts).find(k => counts[k] % 2) || undefined;
}

Tags:

Misc Example