Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goyuninfo/d8305b3ffd6ac28cad3738c4ef416d18 to your computer and use it in GitHub Desktop.
Save goyuninfo/d8305b3ffd6ac28cad3738c4ef416d18 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.BitSet;
/**
* Find missing elements in a Integer array containing numbers
*/
public class I88CA {
public static void main(String args[]) {
findMissingNumber(new int[]{1, 2, 3, 4, 6, 9, 8}, 10);
}
/**
* A general method to find missing values from an integer array. This
* method will work even if array has more than one missing element.
*/
private static void findMissingNumber(int[] numbers, int count) {
int missingCount = count - numbers.length;
BitSet bitSet = new BitSet(count);
for (int number : numbers) {
bitSet.set(number - 1);
}
System.out.printf("Missing numbers in integer array %s, with total number %d is %n",
Arrays.toString(numbers), count);
int lastMissingIndex = 0;
for (int i = 0; i < missingCount; i++) {
lastMissingIndex = bitSet.nextClearBit(lastMissingIndex);
System.out.println(++lastMissingIndex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment