Once Twice - Problem
You are given an integer array nums with the following properties:
- Exactly one element appears once
- Exactly one element appears twice
- All other elements appear exactly three times
Return an integer array of length 2, where:
- The first element is the one that appears once
- The second element is the one that appears twice
Your solution must run in O(n) time and O(1) space.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,1,3,1,3,3,2]
›
Output:
[1,2]
💡 Note:
Element 1 appears once, element 2 appears twice, element 3 appears three times. Return [1,2].
Example 2 — Different Numbers
$
Input:
nums = [4,4,4,5,6,5]
›
Output:
[6,5]
💡 Note:
Element 6 appears once, element 5 appears twice, element 4 appears three times. Return [6,5].
Example 3 — Minimum Length
$
Input:
nums = [0,1,0,0,2,2]
›
Output:
[1,2]
💡 Note:
Element 0 appears three times, element 1 appears once, element 2 appears twice. Return [1,2].
Constraints
- 3 ≤ nums.length ≤ 3 × 104
- -231 ≤ nums[i] ≤ 231 - 1
- Each integer appears either 1, 2, or 3 times
- Exactly one integer appears once
- Exactly one integer appears twice
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code