-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissingNumberFinder.java
24 lines (19 loc) · 1001 Bytes
/
MissingNumberFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Finding the Missing Number in an Array: Given an array with one number missing, this algorithm finds that missing number.
// You use bitwise XOR to cancel out matching elements between the array and the numbers from 0 to n. The result will be the missing number.
public class MissingNumberFinder {
public static int findMissingNumber(int[] nums) {
// Initialize the result to be the length of the array
int missingNumber = nums.length;
// Iterate through the array and use bitwise XOR
for (int i = 0; i < nums.length; i++) {
// XOR the current element with its index and the result
missingNumber ^= i ^ nums[i];
}
return missingNumber;
}
public static void main(String[] args) {
int[] array = {3, 0, 1};
int missingNumber = findMissingNumber(array);
System.out.println("The missing number is: " + missingNumber);
}
}