Minion4
0
Q:

Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times

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))
1
function findOdd(A) {
    var countOccurencesOfInt = 0;
    for (let i = 0; i < A.length; i++) {
        var currentIterationInt = A[i];
        for (let j = 0; j < A.length; j++) {
            if (currentIterationInt == A[j]) {
                countOccurencesOfInt++;
            }
        }
        if (countOccurencesOfInt % 2 != 0) {
            return currentIterationInt;
        }
    }
}
//or
function findOdd(arr) {
  var result, num = 0;

  arr = arr.sort();
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === arr[i+1]) {
      num++;
    } else {
      num++;
      if (num % 2 != 0) {
        result = arr[i];
        break;
      }
    }
  }
  return result;
}
0

New to Communities?

Join the community