How to find the smallest missing number from an unsorted array in Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
 
public class MyClass {
    public static int findSmallestMissingNumber(int[] arr) {
        Set<Integer> st = Arrays.stream(arr).boxed().collect(Collectors.toSet());
 
        int index = 1;
        while (true) {
            if (!st.contains(index)) {
                return index;
            }
            index++;
        }
    }
  
    public static void main(String args[]) {
        int[] arr = { 1, 5, 2, 7, 4, 9, 12 };
  
        System.out.println(findSmallestMissingNumber(arr));
    }
}
 
 
 
 
/*
run:
 
3
 
*/

 



answered Nov 21, 2021 by avibootz
edited Oct 1, 2022 by avibootz
...