# Java Program to Find the Lost Number

Lost Number is the number which is missing from a continuous stream of elements or in an Array. 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 a lost number in an array

Lost number is a number which is missing from a continuous sequence of a numbers in an array.

Consider an array;

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

In the above array ‘arr’, 7 is missing, so 7 is the lost number

Example 2

Consider an array;

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

In the above array 'arr', 10 is missing, so 10 is the lost number

Now, we will discuss various approaches in java to find the lost number in a stream.

## Approach 1: Using stream() and sum() methods

In this approach, we use the stream() function and convert array to stream and then calculate sum of stream using sum() function and store in ‘actualsum’ variable, then we calculate the expectedsum using formula n*(n+1)/2 , then we find lost number using expectedsum - actualsum.

### Algorithm

• Initialize an array with some values.

• Calculate the sum of array using stream() and sum() methods

• Calculate length of array and find the expected sum of the consecutive numbers using sum of n terms formula.

• Subtract the expected value and sum, assign it to a variable and print it.

Stream() − ‘Stream()’ method is used to create stream of elements so that we can use methods like filter(),map(),reduce() to process data

Arrays.stream(collection)


sum() − This method is used to calculate sum of all the elements in a collection.

stream.sum()


### Example

In this example, we will use the stream() and sum() methods to find the lost number using java.

import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 5};
int sum = Arrays.stream(array).sum();
int n = array.length + 1;
int expectedvalue = (n * (n + 1)) / 2;
int lostnumber = expectedvalue - sum;
System.out.println("lost number " + lostnumber);
}
}


### Output

lost number 4


## Approach 2: Using XOR

In this approach, we calculate the XOR of n values and store in expectedValue variable and then we calculate the actualValue of XOR and at last we perform the XOR between between expectedValue and actualValue to obtain the lost number.

### Algorithm

• Initialize an array with some values.

• Calculate length of array and add 1 because I we as the actual numbers in the array should be array.length+1 and assign to a variable 'n'.

• Set expected value as 1 and calculate the expected value using XOR operator using for loop until n.

• Set expected value as array and calculate the actual value using XOR operator using for loop for the elements present in the array.

• Calculate lost number using XOR operator on expected value and actual value and print it

XOR operation (^) − XOR operation performs bitwise operation where if both bits are one it returns 1, else 0. It is represented by ^.

A ^ b // where 'a' and 'b' are integers.


### Example

In this example, we will use the XOR operator and find the lost number using java.

public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 5}; // input array with missing number
int n = array.length + 1; // total number of elements if no number was missing
int expectedValue = 1; // expected XOR value if no number was missing
for (int i = 2; i <= n; i++) {
expectedValue ^= i; // XOR all elements from 1 to n to get expected value
}
for (int i = 1; i < array.length; i++) {
actualValue ^= array[i]; // XOR all elements of array to get actual value
}
int lostNumber = expectedValue ^ actualValue; // XOR expected and actual values to get lost number
System.out.println("The lost number is " + lostNumber);
}
}


### Output

The lost number is 4


## Approach 3: Using HashSet

In this example, we will use the data structure Hashset and inbuilt methods of Hashset to find the lost number using java.

### Algorithm

• Initialize an array with some values.

• Create a hashset and using for loop iterate the array and add the values to hashset.

• Using for loop, iterate over i to array.length+1 and check the missing value in the set using contains() method and print the lost number.

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

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


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

setObjName.contains(value)


### Example

In this approach, we store all the elements of an array in HashSet and we then iterate from 1 to array.length+1 values and check if all the values are present in set if any value is not present then that is the lostnumber value and we print it.

import java.util.Arrays;
import java.util.HashSet;

public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 5};
HashSet<Integer> set = new HashSet<Integer>();
for (int i : array) {
}
for (int i = 1; i <= array.length + 1; i++) {
if (!set.contains(i)) {
System.out.println("lost number: " + i);
break;
}
}
}
}


### Output

lost number: 4


Thus, in this article we have learned different approaches to find the lost Number using java programming language.

Updated on: 16-Aug-2023

70 Views 