Finding smallest positive natural number which is not in an int Array
Jun 12, 2022
package com.celal.test;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class tester {public static void main(String[] args) {int [] data= { -3, 1, 3,0, 6, 4, 1, 2 };
Integer[] myArray= Arrays.stream(data).boxed().toArray( Integer[]::new );List<Integer> mainList = new ArrayList<Integer>(Arrays.asList(myArray));Collections.sort(mainList);int k = mainList.stream().distinct().filter(n -> n > 0).reduce((x, y) -> x + 1 != y ? x : y).orElse(0);System.out.println(k + 1);}}