Skip to content

Instantly share code, notes, and snippets.

@kuanyingchou
Created November 24, 2015 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuanyingchou/c354d553ce2c7cc531fa to your computer and use it in GitHub Desktop.
Save kuanyingchou/c354d553ce2c7cc531fa to your computer and use it in GitHub Desktop.
import java.util.HashSet;
class NotInArray {
public static void main(String[] args) {
int[] input = new int[] { Integer.MAX_VALUE, 3, 4, 7, 9, Integer.MIN_VALUE };
System.out.println(notInArray(input));
}
private static int notInArray(int[] input) {
HashSet<Integer> possibleValues = new HashSet<>();
for(int i = 0; i<input.length; i++) {
int current = input[i];
if(current > Integer.MIN_VALUE && !possibleValues.contains(current-1))
possibleValues.add(current-1);
if(current < Integer.MAX_VALUE && !possibleValues.contains(current+1))
possibleValues.add(current+1);
if(possibleValues.contains(current)) possibleValues.remove(current);
}
if(possibleValues.size() > 0) return possibleValues.iterator().next();
else throw new RuntimeException("Oh oh!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment