From 8abde9c805c992d253921e071b093289efc4b31b Mon Sep 17 00:00:00 2001 From: CharuDaisha <50914191+CharuDaisha@users.noreply.github.com> Date: Thu, 16 Jun 2022 09:08:15 -0700 Subject: [PATCH] Update MissingElementInAnArray.java --- .../classroom/arrays/MissingElementInAnArray.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/week1/day2/classroom/arrays/MissingElementInAnArray.java b/week1/day2/classroom/arrays/MissingElementInAnArray.java index 1fddea5..0c56075 100644 --- a/week1/day2/classroom/arrays/MissingElementInAnArray.java +++ b/week1/day2/classroom/arrays/MissingElementInAnArray.java @@ -6,11 +6,17 @@ public static void main(String[] args) { // Here is the input int[] arr = {1,2,3,4,7,6,8}; - // Sort the array + // Sort the array + + Arrays.sort(arr); // loop through the array (start i from arr[0] till the length of the array) - +for (int i=1;i<=arr.length;i++){ + if(i!=arr[i-1]); + System.out.println( "Missing no is"+ i); + break; +} // check if the iterator variable is not equal to the array values respectively // print the number @@ -22,4 +28,4 @@ public static void main(String[] args) { } -} \ No newline at end of file +}