Code Wars: Find the odd int

https://www.codewars.com/kata/54da5a58ea159efa38000836/train/javascript

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.


function findOdd(A) {
  let arr = A.sort((a,b) => a - b);
  let counter = 1;
 
  if (arr.length === 1) return arr[0];
 
  for (let i = 0; i < arr.length - 1; i++) {
   
    if (arr[i] === arr[i + 1]) {
      counter++;
    }
    else if (arr[i] !== arr[i + 1]) {
     
      if (counter % 2 !== 0) {
        return arr[i];
      }
      // if it turns out to be last number in array with a count of 1 (since for loop doesnt get to it)
      else if (i + 1 === arr.length - 1) {
        return arr[i + 1];
      }
      counter = 1;
    }
  }
 
}

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)