Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,3,2] Output: 3
Example 2:
Input: nums = [0,1,0,1,0,1,99] Output: 99
Constraints:
1 <= nums.length <= 3 * 104-231 <= nums[i] <= 231 - 1nums appears exactly three times except for one element which appears once.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the single number involves checking each number against every other number. We're looking for the number that appears only once while all others appear exactly three times. It's like counting how many times each number shows up and picking the one with a count of one.
Here's how the algorithm would work step-by-step:
def single_number_brute_force(numbers):
for current_number in numbers:
number_of_appearances = 0
for number in numbers:
if number == current_number:
number_of_appearances += 1
# If the number appears only once, return it.
if number_of_appearances == 1:
return current_number
# If the number appears three times, continue to the next number
elif number_of_appearances == 3:
continue
# This will be reached if there is invalid input
return NoneThe core idea is to track the occurrence of each number's bits without using extra storage for counting each number directly. We achieve this by cleverly using two variables to keep track of which bits have appeared once, twice, or (implicitly) three times. Once a bit appears three times, it's effectively reset, allowing us to isolate the bits that appear only once overall.
Here's how the algorithm would work step-by-step:
def single_number_ii(numbers: list[int]) -> int:
seen_once = 0
seen_twice = 0
for number in numbers:
# If seen_once has the bit, it's the second time.
seen_twice |= seen_once & number
# Update seen_once with bits that haven't been seen.
seen_once ^= number
# Find bits seen three times, clear them in both.
common_bit_mask = ~(seen_once & seen_twice)
seen_once &= common_bit_mask
seen_twice &= common_bit_mask
return seen_once| Case | How to Handle |
|---|---|
| Empty or null input array | Return 0 or throw an IllegalArgumentException, depending on the requirements. |
| Array with only one element | Return the single element directly since it must be the unique number. |
| Array with all elements the same | Return 0 since no single number exists; if error condition is allowed, throw an exception. |
| Very large array (close to memory limit) | The bit manipulation approach scales well with large arrays avoiding extra memory. |
| Array contains negative numbers | Bit manipulation works correctly with negative integers through their two's complement representation; other solutions need to handle the sign correctly. |
| Array contains zero | Zero is treated like any other number and handled correctly by the bit manipulation approach. |
| Extreme boundary values (Integer.MAX_VALUE, Integer.MIN_VALUE) | Bit manipulation is robust to these extreme values as each bit is processed independently. |
| Integer overflow potential during intermediate calculations in alternative solutions (e.g., summing) | Bit manipulation or appropriate data types (long) should be used to avoid overflow if summing or other calculation-based solutions are used. |