Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 942 Bytes

find-the-unique-number.md

File metadata and controls

34 lines (25 loc) · 942 Bytes

Find the unique number 6 Kyu

LINK TO THE KATA - FUNDAMENTALS ALGORITHMS ARRAYS

Description

There is an array with some numbers. All numbers are equal except for one. Try to find it!

findUniq([1, 1, 1, 2, 1, 1]) === 2
findUniq([0, 0, 0.55, 0, 0]) === 0.55

It’s guaranteed that array contains at least 3 numbers.

The tests contain some very huge arrays, so think about performance.

Solution

const findUniq = arr => {
  const firstThreeNumbersOrdered = arr.slice(0, 3).sort((a, b) => a - b)
  const repeatedNumber =
    firstThreeNumbersOrdered[1] === firstThreeNumbersOrdered[2]
      ? firstThreeNumbersOrdered[2]
      : firstThreeNumbersOrdered[0]

  return arr.filter(number => number != repeatedNumber)[0]
}