Find the odd int

Codewars kata

by trentHarlem

HTML

<p>
Given an array of integers, find the one that appears an odd number of times.<br/>

There will always be only one integer that appears an odd number of times.<br/>

Examples<br/>
[7] should return 7, because it occurs 1 time (which is odd).<br/>
[0] should return 0, because it occurs 1 time (which is odd).<br/>
[1,1,2] should return 2, because it occurs 1 time (which is odd).<br/>
[0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).<br/>
[1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time (which is odd).<br/>
</p>

JavaScript

//Example Data

let arr = [7] // should return 7, 
let arr1 = [0] // should return 0,
let arr2 = [1, 1, 2] // should return 2,
let arr3 = [0, 1, 0, 1, 0] // should return 0, 
let arr4 = [1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 2, 2, 1] // 4

const findOdd = a => {
  const obj = a.reduce((o, c) => {
    o[c] = o[c] ? o[c] + 1 : 1
    return o 
     },{})
     return Number(Object.keys(obj).filter((key) => (obj[key] % 2)).join());
}
/*   function getKey(obj) {
    const answer = [];
    Object.keys(obj).forEach((key) => {
      if (obj[key] % 2) answer.push(key);
    });
    return Number(answer);
  }
  return getKey(obj)
  } */

console.log(findOdd(arr),findOdd(arr1),findOdd(arr2),findOdd(arr3),findOdd(arr4))

/* const findOdd = a => {
  const obj = a.reduce((o, c) => {
    o[c] = o[c] ? o[c] + 1 : 1
    return o
  }, {})

  function getKey(obj) {
    const answer = [];
    Object.keys(obj).forEach((key) => {
      if (obj[key] % 2) answer.push(key);
    });
    return Number(answer);
  }
  return getKey(obj)
} */