Write a function(int[]) -> int that returns the lowest unassigned integer. For example [] -> 1 (empty set), [1] -> 2, [5, 3, 1] -> 2. Basically just sort the array, iterate, and compare current and previous. If there is a gap then that's your number.
Anonymous
private static int getlowest(int[] arr) { if (arr.length == 0) { return 1; } if (arr.length == 1) { return arr[0] + 1; } Arrays.sort(arr); int prev; int current = 0; for (int item : arr) { prev = current; current = item; if (current - prev > 1) { return prev + 1; } } return current + 1; }
Check out your Company Bowl for anonymous work chats.