DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
willsmart profile image
willsmart • Edited

A javascript one that has pretty reasonable complexity

function findOutlier(integers){
  // The first three ints can be used to determine whether we are 
  // looking for an odd or an even
  // If at least two are even, then we're looking for the odd integer
  const typicalIntegerIsEven = isEven(integers[0]) + isEven(integers[1]) + isEven(integers[2]) >= 2
  // Return the first integer that is non-typical
  return integers.find(v => isEven(v) != typicalIntegerIsEven)

  // Helper function to determine if a number is even or odd
  // Note that (-1)%2 === -1 and some integers might be negative.
  // (-2)%2 === -0 and -0 === 0, so this check will work for any integer.
  // v&1 is better in many ways, but only goes up to 32 bit ints
  function isEven(v) {return v%2 === 0}
}