DSA · Bit Manipulation · #164
Single Number II
Module 50 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given an integer array `nums` where every element appears **exactly three times** except for one element which appears **exactly once**, return that single element. Implement the function `singleNumber(nums)`. You must design an algorithm with linear runtime complexity and use only constant extra space.
Examples
nums = [2,2,3,2]→3— 3 appears once; 2 appears three times.nums = [0,1,0,1,0,1,99]→99— 99 is the only element not appearing three times.nums = [-2,-2,1,-2]→1— Works for negative numbers too.
Constraints
- · 1 <= nums.length <= 3 * 10^4
- · -2^31 <= nums[i] <= 2^31 - 1
- · Each element in nums appears exactly three times except for one element which appears exactly once.
- · Required: O(n) time, O(1) extra space.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.