Skip to content

Commit f119d27

Browse files
MEDIUM/src/medium/MissingNumber.java
1 parent bddcebe commit f119d27

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

MEDIUM/src/medium/MissingNumber.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package medium;
2+
3+
public class MissingNumber {
4+
//This post has a very good explanation: https://discuss.leetcode.com/topic/24535/4-line-simple-java-bit-manipulate-solution-with-explaination
5+
//we can simply XOR with the indices of the array since XOR twice the same number will become zero
6+
public int missingNumber(int[] nums) {
7+
int xor = 0, i = 0;
8+
for(; i < nums.length; i++){
9+
xor = xor^i^nums[i];
10+
}
11+
return xor^i;
12+
}
13+
14+
}

0 commit comments

Comments
 (0)