Skip to content

Commit 5641f8f

Browse files
feat: add Single Number solution
- XOR operation algorithm implementation - Multiple approaches: Hash Set, Mathematical, Sorting, Frequency Map - O(n) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent 21ca7cd commit 5641f8f

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 num : nums) {
10+
result ^= num;
11+
}
12+
13+
return result;
14+
}
15+
}
16+
17+
// Alternative approach using hash set
18+
class SolutionHashSet {
19+
public int singleNumber(int[] nums) {
20+
Set<Integer> set = new HashSet<>();
21+
22+
for (int num : nums) {
23+
if (set.contains(num)) {
24+
set.remove(num);
25+
} else {
26+
set.add(num);
27+
}
28+
}
29+
30+
return set.iterator().next();
31+
}
32+
}
33+
34+
// Alternative approach using mathematical sum
35+
class SolutionMathematical {
36+
public int singleNumber(int[] nums) {
37+
Set<Integer> set = new HashSet<>();
38+
int sum = 0;
39+
int uniqueSum = 0;
40+
41+
for (int num : nums) {
42+
sum += num;
43+
if (set.add(num)) {
44+
uniqueSum += num;
45+
}
46+
}
47+
48+
return 2 * uniqueSum - sum;
49+
}
50+
}
51+
52+
// Alternative approach using iterative
53+
class SolutionIterative {
54+
public int singleNumber(int[] nums) {
55+
int result = 0;
56+
57+
for (int i = 0; i < nums.length; i++) {
58+
result ^= nums[i];
59+
}
60+
61+
return result;
62+
}
63+
}
64+
65+
// Alternative approach using while loop
66+
class SolutionWhileLoop {
67+
public int singleNumber(int[] nums) {
68+
int result = 0;
69+
int i = 0;
70+
71+
while (i < nums.length) {
72+
result ^= nums[i];
73+
i++;
74+
}
75+
76+
return result;
77+
}
78+
}
79+
80+
// Alternative approach using enhanced for loop
81+
class SolutionEnhancedForLoop {
82+
public int singleNumber(int[] nums) {
83+
int result = 0;
84+
85+
for (int num : nums) {
86+
result ^= num;
87+
}
88+
89+
return result;
90+
}
91+
}
92+
93+
// Alternative approach using recursive
94+
class SolutionRecursive {
95+
public int singleNumber(int[] nums) {
96+
return singleNumberHelper(nums, 0, 0);
97+
}
98+
99+
private int singleNumberHelper(int[] nums, int index, int result) {
100+
if (index >= nums.length) {
101+
return result;
102+
}
103+
104+
return singleNumberHelper(nums, index + 1, result ^ nums[index]);
105+
}
106+
}
107+
108+
// Alternative approach using sorting
109+
class SolutionSorting {
110+
public int singleNumber(int[] nums) {
111+
Arrays.sort(nums);
112+
113+
for (int i = 0; i < nums.length - 1; i += 2) {
114+
if (nums[i] != nums[i + 1]) {
115+
return nums[i];
116+
}
117+
}
118+
119+
return nums[nums.length - 1];
120+
}
121+
}
122+
123+
// Alternative approach using frequency map
124+
class SolutionFrequencyMap {
125+
public int singleNumber(int[] nums) {
126+
Map<Integer, Integer> freq = new HashMap<>();
127+
128+
for (int num : nums) {
129+
freq.put(num, freq.getOrDefault(num, 0) + 1);
130+
}
131+
132+
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
133+
if (entry.getValue() == 1) {
134+
return entry.getKey();
135+
}
136+
}
137+
138+
return -1; // Should never reach here
139+
}
140+
}
141+
142+
// More concise version
143+
class SolutionConcise {
144+
public int singleNumber(int[] nums) {
145+
int result = 0;
146+
for (int num : nums) result ^= num;
147+
return result;
148+
}
149+
}

0 commit comments

Comments
 (0)