Find the Missing Number in an Array (JavaScript Interview Question)

Learn how to solve the Find the Missing Number in an Array interview question using JavaScript. This beginner-friendly guide explains the logic step by step with simple examples and clean code, making it perfect for coding interviews and problem-solving practice.

JAVASCRIPTALL

Adarsh Bharadwaj S

12/13/20252 min read

If you’ve ever started preparing for coding interviews, chances are you’ve already come across the “Find the Missing Number in an Array” interview question. It looks very basic at first, but interviewers love it because it tests how clearly you think, not how fast you type.

In this blog, I’ll explain the problem step by step, using simple language, real examples, and clean JavaScript code — exactly how a developer would explain it to another developer.

Interview Problem Statement

You are given an array that should contain numbers from 1 to N, but one number is missing.

Input: [1, 2, 4, 5]

Output: 3

Your task is to find the missing number efficiently, without unnecessary loops or extra memory.

Why This Problem Is Asked So Often

This interview question helps interviewers understand whether you:

  • Understand arrays

  • Can apply basic math

  • Think about time and space complexity

  • Avoid unnecessary operations

That’s why it appears in interviews for JavaScript, React, React Native, and general DSA roles.

Approach 1: Using a Simple Math Formula (Beginner Friendly)

There’s a well-known formula to calculate the sum of numbers from 1 to N:

Sum = N × (N + 1) / 2

If one number is missing, the difference between the expected sum and the actual sum of the array gives us the answer.

Example Walkthrough

Array: [1, 2, 4, 5]
N = 5

  • Expected Sum = 5 × 6 / 2 = 15

  • Actual Sum = 1 + 2 + 4 + 5 = 12

  • Missing Number = 15 - 12 = 3

JavaScript Code

function findMissingNumber(arr, n) {

const expectedSum = (n * (n + 1)) / 2;

const actualSum = arr.reduce((sum, num) => sum + num, 0);

return expectedSum - actualSum;

}

console.log(findMissingNumber([1, 2, 4, 5], 5)); // 3

Time Complexity: O(n)
Space Complexity: O(1)

This is the best approach for beginners and works perfectly in most cases.

Approach 2: XOR Method (Interview Favorite)

If you want to impress in interviews, this approach is worth knowing.

Why XOR Works

XOR has a special property:

  • a ⊕ a = 0

  • a ⊕ 0 = a

When we XOR all numbers from 1 to N and also XOR all elements in the array, all matching numbers cancel out — leaving only the missing number.

JavaScript Code

function findMissingUsingXOR(arr, n) {

let xor1 = 0;

let xor2 = 0;

for (let i = 1; i <= n; i++) xor1 ^= i;

for (let num of arr) xor2 ^= num;

return xor1 ^ xor2;

}

console.log(findMissingUsingXOR([1, 2, 4, 5], 5)); // 3

This method is fast, memory-efficient, and avoids overflow issues.

Common Mistakes Beginners Make

  • Sorting the array (unnecessary and slow)

  • Using nested loops

  • Creating extra arrays or objects

  • Ignoring time complexity

Simple logic beats complicated code — always.

Which Approach Should You Use?

  • Learning phase: Math formula

  • Interviews: XOR method

  • Large inputs: XOR method

Both are correct — knowing why they work matters more.

Final Thoughts

The Missing Number problem is small, but mastering it builds confidence in problem solving. Once you understand this, many other array problems start to feel easier.

If you’d like more simple, real-world coding explanations, stay tuned — more problem-solving blogs are coming.

Find the Missing Number in an Array | Easy Coding Interview Problem Explained | Hindi |