MediumFoundational
Find the Number That Appears Once
- Time limit
- 1.5 s
- Memory limit
- 128 MB
- Estimated
- 25 min
- Bit Manipulation
Description
You are given an integer array nums. Exactly one value appears once, while every other distinct value appears exactly three times. Return the value that appears once.
The array may contain negative values, zero, and positive values. The order of elements is arbitrary. You may not sort the array as a way to solve the problem; aim for a solution whose running time is linear in the array length and whose extra space does not grow with the array length.
Example 1: nums = [2, 2, 3, 2] returns 3 because 2 appears three times and 3 appears once.
Example 2: nums = [0, 1, 0, 1, 0, 1, 99] returns 99.
Example 3: nums = [-2, -2, -2, -7] returns -7.
Constraints
- 1 <= nums.length <= 100
- nums.length % 3 == 1
- Exactly one distinct value appears once; every other distinct value appears exactly three times.
- -2^31 <= nums[i] <= 2^31 - 1
- A linear-time solution with constant auxiliary space is expected.
Sample cases
3Sample 1
Input
nums = [2,2,3,2]
Expected output
3
Sample 2
Input
nums = [0,1,0,1,0,1,99]
Expected output
99
Skills and languages
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer