AlgoDaily

Single Lonely Number

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Description

In a given array of numbers, one element will show up once and the others will each show up twice. Can you find the number that only appears once in O(n) linear time? Bonus points if you can do it in O(1) space as well.

lonelyNumber([4, 4, 6, 1, 3, 1, 3])
// 6

lonelyNumber([3, 3, 9])
// 9
Constraints
  • Length of the array <= 100,000
  • The values of the array will be between -1,000,000,000 and 1,000,000,000
```js
function lonelyNumber(numbers) {
  // Fill in this method
  return numbers;
}

const result = lonelyNumber([4, 4, 6, 1, 3, 1, 3]);
console.log(result);
```

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →