Site icon

Find a missing number in an array of distinct integers in java (example)

Method 1: find a missing number in an integer array using Sum (java)

Program: find a missing number using sum formula in java

private static int missingNumberUsingSum(int[] elements) {

	int arraySum = 0;
	// Sum of array
	for (int element : elements) {
		arraySum += element;
	}

	// sum of number from 1 to n
	int n = elements.length + 1; // as one number is missing
	int sumOfNumbers = (n * (n + 1)) / 2;
	return sumOfNumbers - arraySum;
}

Method 2: find a missing number in an integer array using XOR in java

X Y  Output
0 0  0
0 1 1
0  1
1 0

Program: find a missing number in an integer array using XOR in java

private static int missingNumberUsingXOR(int[] elements) {

	int n = elements.length + 1;
	int missingNumber = elements[0];
	for (int index = 1; index <= n; index++) {
		if (index < elements.length) {
			missingNumber ^= elements[index];
		}
		missingNumber ^= index;
	}
	return missingNumber;
}

Complete program: find a missing number in an integer array (Sum & XOR)

package org.learn.arrays;

import java.util.Arrays;

public class MissingNumber {
	public static void main(String[] args) {

		int elements[] = { 1, 6, 3, 2, 7, 5 };

		String array = Arrays.toString(elements);
		int missingNumber = missingNumberUsingSum(elements);
		System.out.printf("1. Missing number in %s using sum is: %d", array, missingNumber);

		missingNumber = missingNumberUsingXOR(elements);
		System.out.printf("\n2. Missing number in %s using XOR is: %d", array, missingNumber);

		int elements1[] = { 1, 2, 4, 5, 6, 7 };
		array = Arrays.toString(elements1);

		missingNumber = missingNumberUsingSum(elements1);
		System.out.printf("\n3. Missing number in %s using sum is: %d", array, missingNumber);

		missingNumber = missingNumberUsingXOR(elements1);
		System.out.printf("\n4. Missing number in %s using XOR is: %d", array, missingNumber);

		int elements2[] = { 7, 6, 4, 3, 2, 1 };
		array = Arrays.toString(elements2);

		missingNumber = missingNumberUsingSum(elements2);
		System.out.printf("\n5. Missing number in %s using sum is: %d", array, missingNumber);

		missingNumber = missingNumberUsingXOR(elements2);
		System.out.printf("\n6. Missing number in %s using XOR is: %d", array, missingNumber);
	}

	private static int missingNumberUsingXOR(int[] elements) {

		int n = elements.length + 1;
		int missingNumber = elements[0];
		for (int index = 1; index <= n; index++) {
			if (index < elements.length) {
				missingNumber ^= elements[index];
			}
			missingNumber ^= index;
		}
		return missingNumber;
	}

	private static int missingNumberUsingSum(int[] elements) {

		int arraySum = 0;
		// Sum of array
		for (int element : elements) {
			arraySum += element;
		}

		// sum of number from 1 to n
		int n = elements.length + 1; // as one number is missing
		int sumOfNumbers = (n * (n + 1)) / 2;
		return sumOfNumbers - arraySum;
	}
}

Output: find missing number in an array of distinct integers

1. Missing number in [1, 6, 3, 2, 7, 5] using sum is: 4
2. Missing number in [1, 6, 3, 2, 7, 5] using XOR is: 4
3. Missing number in [1, 2, 4, 5, 6, 7] using sum is: 3
4. Missing number in [1, 2, 4, 5, 6, 7] using XOR is: 3
5. Missing number in [7, 6, 4, 3, 2, 1] using sum is: 5
6. Missing number in [7, 6, 4, 3, 2, 1] using XOR is: 5
Exit mobile version