Java Program to Find the smallest missing number


Smallest missing number is the smallest number which is missing from a stream of elements or in an Array. The stream may or may not contain continuous elements. If the stream is continuous then the smallest missing number is nothing but the Smallest Missing Number in a stream. In this section, we will discuss the various approaches to find a Smallest Missing Number in a stream of elements using Java programming language.

Example for smallest missing number in an array

Example 1

Input

arr=[1,2,3,5,6,7,8]

Output

4

Explanation − In the above array ‘arr’ 4 is missing and 4 is the smallest among them. So 4 is the smallest number. The lost number principle is not applied here as the sequence is not continuous.

Example 2

Input

arr=[1,2,3,5,6,7,8]

Output

4

Explanation − In the above array ‘arr’ 4 is missing and 4 is the smallest among them. so 4 is the smallest number and as the sequence is continuous ‘4’ is also the lost number in the given array.

Approach 1: Using a Loop Statement

In this approach, we initialize an array with some values and then, we call a custom function “missingNumber”, this function generally returns a value that is missing in the array by iterating over the length of the array. If no element is missed in the array, then we return the value which is the first element outside of the array.

Algorithm

  • Initialize an array.

  • Declare a function missingNumber(arr, n).

  • Loop from 0 to n and match with the elements in the array.

  • We will check through the loop if all the elements are present in the array or not. If we find any element not present in the array, return it.

  • Call the function in the main method and print the returned value by the function

Example

In this example, we will be using a for-loop statement provided by Java to find the smallest missing number.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int[] arr = { 0, 1, 2, 3, 4, 5, 8,11 };
      int missing_number = missingNumber(arr, arr.length);
      System.out.println("The smallest missing number " + missing_number);
   }
   static int missingNumber(int[] arr, int n) {
      int i;
      for (i = 0; i < n; i++) {
         if (arr[i] != i) {
            return i;
         }
      }
      return i;
   }
}

Output

The smallest missing number 6

Approach 2: Using HashSet in Java

In this approach, we initialize an array with some values and then, we call a custom function “missingNumber”, this function generally returns a value which is missing in the array by storing all the elements in HashSet and then we iterate over the length of array and check if every value is present in set if not present we return that value else if every element is present at last we return n as it is the first number missed.

Algorithm

  • Initialize an array arr.

  • Declare a function missingNumber(arr, n).

  • Declare a HashSet and add all the elements of the array to the set using add().

  • Loop through 0 to n and find if the value is present in hashset using contains() and if value is not present return it.

  • Call the function in main method and print the returned value by the method.

HashSet − Hashset is an unordered collection of objects that does not allow duplicate elements.

HashSet<datatype> objName = new HashSet<datatype>();	

contains() − This methods check whether a value is present in the set or not and returns a boolean value.

setObjName.contains(value)

Example

In this example, we will be using the HashSet collection in java and different inbuilt methods of HashSet and find the smallest missing number.

import java.util.HashSet;
public class Main {
   public static void main(String[] args) {
      int[] arr = { 0, 1, 2, 3, 4, 5, 8,11 };
      int missing_number = missingNumber(arr, arr.length);
      System.out.println("The smallest missing number is " + missing_number);
   }
   static int missingNumber(int[] arr, int n) {
      HashSet<Integer> set = new HashSet<>();
      for (int i = 0; i < n; i++) {
         set.add(arr[i]);
      }
      for (int i = 0; i < n; i++) {
         if (!set.contains(i)) {
            return i;
         }
      }
      return n;
   }
}

Output

The smallest missing number is 6

Thus, in this article we have discuss different approaches to find the smallest missing number using Java programming language.

Updated on: 25-Jun-2024

643 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements