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 be contain continuous elements. If the stream is continuous then the smallest missing number is nothing but the Lost Number in a stream. In this section, we will discuss the various approaches to find a lost 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, 7 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.

Array Initialization

datatype arrayName[] = {values};
arrayName[index]=value;

Example

int array[] = {1, 2, 3, 4, 5}; //Initializing whole array
array[0]=1; //Initializing a particular value

To access an array

  • The array[index] can be used to access elements in an array where index starts from 0.

  • The array[0] represents the first element in the array,array[1] represents the second element and so on.

Now, we will discuss in detail using java programs how to find the smallest missing number.

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 collectionin java and different inbuilt methods of hasset 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: 16-Aug-2023

345 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements