xiqe / code-train

前端算法

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Find the odd int

xiqe opened this issue · comments

Find the odd int

Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

Examples:

findOdd([1,2,2,3,3,1,4,5,5]) //5
findOdd([10]) //10
findOdd([7,7,7,1,2,1,2]) //7

reply

function findOdd(A) {
  let arr = [];
  for(let i=0;i<A.length;i++){
    let index = arr.indexOf(A[i]);
    if(index>-1){
      arr.splice(index,1)
    } else {
      arr.push(A[i])
    }
  }
  return arr[0];
}