How to find a missing number in an array ? | unsorted Array Missing Number

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we are going to see the most frequently asked interview question, how to find a missing number in an array sorted/unsorted.
Input: numbers={1,2,3,5,6}
Output: 4
numbers={1,3,2,5,6}
Output: 4
We can find a missing number in an array in different approaches, let’s see the easiest ways.
Here I am going to show the simplest way to find a missing number in a sorted array.
Sort an Array using Arrays.sort(arr).
Since Array is sorted, we can compare the array indexes with the values.
If anyone of the array value not equal to array Index, then we can say that, that is a missed number.
Let’s see in practice.
import java.util.Arrays;

public class MissingNumber {

    public static void main(String[] args) {
        int[] number = {6, 3, 2, 4, 1};
        Arrays.sort(number);
        System.out.println("Missing Number is:" + getMissingNumber(number));
    }

    private static Integer getMissingNumber(int[] number) {
        Integer missed = null;
        for (int i = 0; i < number.length; i++) {
            int index = i + 1;
            if (number[i] != index) {
                missed = index;
                break;
            }
        }
        return missed;
    }
}
Missing Number is:5
We can even find a missing number in an unsorted array in a simple approach using the formula n*(n+1)/2.
Calculate the sum of number using (n+1) * (n+2)/2
Loop through all the elements from the array and subtract all the numbers form the sum.
Then you will get the missed number.
public class MissingNumber {

    public static void main(String[] args) {
        int[] number = {6, 3, 2, 4, 1};
        System.out.println("Missing Number is:" + getMissingNumber(number));
    }
    public static int getMissingNumber(int arr[])
    {
        int i, total;
        int n = arr.length;
        total = (n + 1) * (n + 2) / 2;
        for (i = 0; i < n; i++)
            total -= arr[i];
        return total;
    }
}
Missing Number is:5
Happy Learning 🙂

How to Sort an Array in Parallel in Java 8
PHP Array Sort Techniques Tutorials
PHP Array Example Tutorials
What is Java Arrays and how it works ?
Selection Sort In Java
C Program to add elements of an array
Java Insertion Sort
Bubble Sort In Java
C Program – Bubble Sort Program in C
String sorting in Java
Java 8 Array contains a specific value ?
How to Convert Java 8 Stream to Array
Using Array in AngularJs Example
Difference between Linkedlist and Array
How to Rotate an Array to Left direction based on user input ?

How to Sort an Array in Parallel in Java 8
PHP Array Sort Techniques Tutorials
PHP Array Example Tutorials
What is Java Arrays and how it works ?
Selection Sort In Java
C Program to add elements of an array
Java Insertion Sort
Bubble Sort In Java
C Program – Bubble Sort Program in C
String sorting in Java
Java 8 Array contains a specific value ?
How to Convert Java 8 Stream to Array
Using Array in AngularJs Example
Difference between Linkedlist and Array
How to Rotate an Array to Left direction based on user input ?
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String