Good evening! Here's our prompt for today.
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.

SNIPPET
1lonelyNumber([4, 4, 6, 1, 3, 1, 3])
2// 6
3
4lonelyNumber([3, 3, 9])
5// 9
Constraints
- Length of the array <=
100,000
- The values of the array will be between
-1,000,000,000
and1,000,000,000
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
30
​
def lonely_number(numbers):
# fill in
return numbers
​
​
import unittest
​
​
class Test(unittest.TestCase):
def test_1(self):
assert lonely_number([4, 4, 6, 1, 3, 1, 3]) == 6
print("PASSED: `lonely_number([4, 4, 6, 1, 3, 1, 3])` should return `6`")
​
def test_2(self):
assert lonely_number([3, 3, 9]) == 9
print("PASSED: `lonely_number([3, 3, 9])` should return `9`")
​
def test_3(self):
assert callable(lonely_number) == True
print("PASSED: `lonely_number` is a function")
​
def test_4(self):
assert lonely_number([3, 3, 9]) == 9
print("PASSED: `lonely_number([1])` should return `1`")
​
​
if __name__ == "__main__":
unittest.main(verbosity=2)
print("Nice job, 4/4 tests passed!")
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Tired of reading? Watch this video explanation!
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's how we would solve this problem...
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.