
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Smallest Missing Number in Java
The smallest missing number is the smallest number that 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.
Problem Statement
Given a stream of integers find the smallest missing number from the stream.
Input 1
arr=[1,2,3,5,6,7,8]
Output 1
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.
Input 2
arr=[1,2,3,5,6,7,8]
Output 2
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.
Different Approaches
Following are the approaches to finding 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.
Steps to follow
Below are the steps to find the smallest missing number using the loop statement ?
-
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 that is missing in the array by storing all the elements in HashSet and then we iterate over the length of the array and check if every value is present in the 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.
Steps to follow
Below are the steps to find the smallest missing number using HashSet ?
-
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 the value is not present return it.
-
Call the function in the 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 discussed different approaches to finding the smallest missing number using Java programming language.