Find Missing Number in Java 8 Array

TodayCoding Find the missing number in array using java8 ? public class{ public static void main(String args[]){ int[] arr={1,1,2,2,3,4,5,5,6,7,8,10}; int MissingNumber=IntStream.rangeClosed(1,10) .filter(num->Arrays.stream(arr).noneMatch(a->a==num)) .findFirst() .orElse(-1); System.out.println(MissingNumber); } } Logic:- 1. generate numbers from 1 to 10 2.keep only numbers that is not in the array 3.find the first number 4.if no number found give default value is -1 IntStream.rangeClosed(1,10)------->it will generate the numbers from 1 to 10 inclusive; .filter()--->it will filter the given stream noneMatch()--> if no element match it will return true if atleast one element match it return false .findFirst()--> give first element in stream .orElse(-1)---> give default value #TodayCoding #Java8Coding #CodingPractise #InterviewPrep #Consistency

To view or add a comment, sign in

Explore content categories