Skip to content

Commit f5f76fa

Browse files
feat: add Single Number II solution
- Bit manipulation algorithm implementation - Multiple approaches: Hash Map, Sorting, Recursive, Mathematical - O(n) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent 6ca4700 commit f5f76fa

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* Time Complexity: O(n) - Single pass
3+
* Space Complexity: O(1) - No extra space
4+
*/
5+
class Solution {
6+
public int singleNumber(int[] nums) {
7+
int result = 0;
8+
9+
for (int i = 0; i < 32; i++) {
10+
int count = 0;
11+
12+
for (int num : nums) {
13+
if (((num >> i) & 1) == 1) {
14+
count++;
15+
}
16+
}
17+
18+
if (count % 3 != 0) {
19+
result |= (1 << i);
20+
}
21+
}
22+
23+
return result;
24+
}
25+
}
26+
27+
// Alternative approach using hash map
28+
class SolutionHashMap {
29+
public int singleNumber(int[] nums) {
30+
Map<Integer, Integer> count = new HashMap<>();
31+
32+
for (int num : nums) {
33+
count.put(num, count.getOrDefault(num, 0) + 1);
34+
}
35+
36+
for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
37+
if (entry.getValue() == 1) {
38+
return entry.getKey();
39+
}
40+
}
41+
42+
return -1; // Should never reach here
43+
}
44+
}
45+
46+
// Alternative approach using sorting
47+
class SolutionSorting {
48+
public int singleNumber(int[] nums) {
49+
Arrays.sort(nums);
50+
51+
for (int i = 0; i < nums.length - 2; i += 3) {
52+
if (nums[i] != nums[i + 1]) {
53+
return nums[i];
54+
}
55+
}
56+
57+
return nums[nums.length - 1];
58+
}
59+
}
60+
61+
// Alternative approach using iterative
62+
class SolutionIterative {
63+
public int singleNumber(int[] nums) {
64+
int result = 0;
65+
66+
for (int i = 0; i < 32; i++) {
67+
int count = 0;
68+
69+
for (int j = 0; j < nums.length; j++) {
70+
if (((nums[j] >> i) & 1) == 1) {
71+
count++;
72+
}
73+
}
74+
75+
if (count % 3 != 0) {
76+
result |= (1 << i);
77+
}
78+
}
79+
80+
return result;
81+
}
82+
}
83+
84+
// Alternative approach using while loop
85+
class SolutionWhileLoop {
86+
public int singleNumber(int[] nums) {
87+
int result = 0;
88+
int i = 0;
89+
90+
while (i < 32) {
91+
int count = 0;
92+
int j = 0;
93+
94+
while (j < nums.length) {
95+
if (((nums[j] >> i) & 1) == 1) {
96+
count++;
97+
}
98+
j++;
99+
}
100+
101+
if (count % 3 != 0) {
102+
result |= (1 << i);
103+
}
104+
105+
i++;
106+
}
107+
108+
return result;
109+
}
110+
}
111+
112+
// Alternative approach using enhanced for loop
113+
class SolutionEnhancedForLoop {
114+
public int singleNumber(int[] nums) {
115+
int result = 0;
116+
117+
for (int i = 0; i < 32; i++) {
118+
int count = 0;
119+
120+
for (int num : nums) {
121+
if (((num >> i) & 1) == 1) {
122+
count++;
123+
}
124+
}
125+
126+
if (count % 3 != 0) {
127+
result |= (1 << i);
128+
}
129+
}
130+
131+
return result;
132+
}
133+
}
134+
135+
// Alternative approach using recursive
136+
class SolutionRecursive {
137+
public int singleNumber(int[] nums) {
138+
return singleNumberHelper(nums, 0, 0);
139+
}
140+
141+
private int singleNumberHelper(int[] nums, int bitPos, int result) {
142+
if (bitPos >= 32) {
143+
return result;
144+
}
145+
146+
int count = 0;
147+
for (int num : nums) {
148+
if (((num >> bitPos) & 1) == 1) {
149+
count++;
150+
}
151+
}
152+
153+
if (count % 3 != 0) {
154+
result |= (1 << bitPos);
155+
}
156+
157+
return singleNumberHelper(nums, bitPos + 1, result);
158+
}
159+
}
160+
161+
// Alternative approach using mathematical
162+
class SolutionMathematical {
163+
public int singleNumber(int[] nums) {
164+
Set<Integer> set = new HashSet<>();
165+
long sum = 0;
166+
long uniqueSum = 0;
167+
168+
for (int num : nums) {
169+
sum += num;
170+
if (set.add(num)) {
171+
uniqueSum += num;
172+
}
173+
}
174+
175+
return (int) ((3 * uniqueSum - sum) / 2);
176+
}
177+
}
178+
179+
// More concise version
180+
class SolutionConcise {
181+
public int singleNumber(int[] nums) {
182+
int result = 0;
183+
for (int i = 0; i < 32; i++) {
184+
int count = 0;
185+
for (int num : nums) {
186+
if (((num >> i) & 1) == 1) count++;
187+
}
188+
if (count % 3 != 0) result |= (1 << i);
189+
}
190+
return result;
191+
}
192+
}

0 commit comments

Comments
 (0)