int answer = 0; Arrays.sort(A); for(int i = 0; i < A.length; i++){ if(answer < A[i] && answer + 1 == A[i]){ answer = A[i]; } } return answer + 1;