0
0
GoProgramBeginner · 2 min read

Go Program to Find Missing Number in Slice

In Go, you can find the missing number in a slice of integers from 1 to n by calculating the expected sum with n*(n+1)/2 and subtracting the actual sum of the slice elements; for example, use missing := totalSum - actualSum.
📋

Examples

Input[1, 2, 4, 5, 6]
Output3
Input[2, 3, 1, 5]
Output4
Input[1]
Output2
🧠

How to Think About It

To find the missing number in a slice containing numbers from 1 to n with one missing, first calculate the total sum of numbers from 1 to n using the formula n*(n+1)/2. Then find the sum of all numbers in the slice. The missing number is the difference between the total sum and the slice sum.
📐

Algorithm

1
Get the length of the slice and add 1 to find n (the full range).
2
Calculate the total sum of numbers from 1 to n using the formula n*(n+1)/2.
3
Calculate the sum of all numbers present in the slice.
4
Subtract the slice sum from the total sum to find the missing number.
5
Return or print the missing number.
💻

Code

go
package main

import "fmt"

func findMissingNumber(nums []int) int {
    n := len(nums) + 1
    totalSum := n * (n + 1) / 2
    actualSum := 0
    for _, num := range nums {
        actualSum += num
    }
    return totalSum - actualSum
}

func main() {
    slice := []int{1, 2, 4, 5, 6}
    missing := findMissingNumber(slice)
    fmt.Println(missing)
}
Output
3
🔍

Dry Run

Let's trace the slice [1, 2, 4, 5, 6] through the code

1

Calculate n

Length of slice is 5, so n = 5 + 1 = 6

2

Calculate total sum

totalSum = 6 * (6 + 1) / 2 = 21

3

Calculate actual sum

Sum of slice elements = 1 + 2 + 4 + 5 + 6 = 18

4

Find missing number

missing = totalSum - actualSum = 21 - 18 = 3

StepValue
n6
totalSum21
actualSum18
missing3
💡

Why This Works

Step 1: Calculate total sum

The formula n*(n+1)/2 gives the sum of all numbers from 1 to n, which is the expected total if no number was missing.

Step 2: Sum slice elements

Adding all numbers in the slice gives the actual sum of present numbers.

Step 3: Subtract to find missing

Subtracting the actual sum from the total sum reveals the missing number because it accounts for the gap.

🔄

Alternative Approaches

Using a boolean map
go
package main

import "fmt"

func findMissingBoolean(nums []int) int {
    n := len(nums) + 1
    present := make([]bool, n+1)
    for _, num := range nums {
        present[num] = true
    }
    for i := 1; i <= n; i++ {
        if !present[i] {
            return i
        }
    }
    return -1
}

func main() {
    slice := []int{1, 2, 4, 5, 6}
    missing := findMissingBoolean(slice)
    fmt.Println(missing)
}
This method uses extra space but is easy to understand and works well for small ranges.
Using XOR operation
go
package main

import "fmt"

func findMissingXOR(nums []int) int {
    n := len(nums) + 1
    xor := 0
    for i := 1; i <= n; i++ {
        xor ^= i
    }
    for _, num := range nums {
        xor ^= num
    }
    return xor
}

func main() {
    slice := []int{1, 2, 4, 5, 6}
    missing := findMissingXOR(slice)
    fmt.Println(missing)
}
XOR method uses constant space and is efficient but less intuitive for beginners.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops once through the slice to sum elements, so time grows linearly with slice size.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The sum formula method is fastest and simplest; XOR is also O(n) but less intuitive; boolean map uses extra space.

ApproachTimeSpaceBest For
Sum formulaO(n)O(1)Simple and fast for numeric ranges
Boolean mapO(n)O(n)Easy to understand, uses extra memory
XOR operationO(n)O(1)Efficient and no extra memory, but less intuitive
💡
Use the sum formula n*(n+1)/2 to quickly find the missing number without extra memory.
⚠️
Forgetting to add 1 to the slice length to get the full range size n causes wrong results.