How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?

Finding the missing number and the repeated number in a sorted array is a common programming problem. This article demonstrates how to solve this problem without using any built-in functions in C#.

The approach uses a boolean array to track which numbers are present and an integer array to count occurrences. By traversing the original array and marking elements in auxiliary arrays, we can identify both the missing and repeated elements efficiently.

Algorithm Overview

The solution works in two main steps −

  • Finding the missing number: Create a boolean array and mark elements as true when found. The first false element represents the missing number.

  • Finding the repeated number: Use an integer array to count occurrences. The first element with count 2 is the repeated number.

Finding Missing and Repeated Numbers Original Array [0, 1, 1, 3, 4] Boolean Array [F, T, F, T, T] Result Missing: 2, Repeated: 1

Example

using System;

namespace ConsoleApplication {
    public class Arrays {
        public void MissingNumberAndRepeatedNumber(int[] arr) {
            bool[] tempArray = new bool[arr.Length + 1];
            int missingelement = -1;
            int repeatingelement = -1;
            
            // Mark elements as found in boolean array
            for (int i = 0; i < arr.Length; i++) {
                int index = arr[i];
                if (!tempArray[index]) {
                    tempArray[index] = true;
                }
            }
            
            // Find missing element (first false in boolean array)
            for (int i = 0; i < tempArray.Length; i++) {
                if (!tempArray[i]) {
                    missingelement = i;
                    break;
                }
            }
            
            // Use integer array to count occurrences
            int[] tempArray1 = new int[arr.Length + 1];
            for (int i = 0; i < arr.Length; i++) {
                int index = arr[i];
                if (tempArray1[index] == 0) {
                    tempArray1[index] = 1;
                } else if (tempArray1[index] == 1) {
                    tempArray1[index] = 2;
                }
            }
            
            // Find repeated element (first element with count 2)
            for (int i = 0; i < tempArray1.Length; i++) {
                if (tempArray1[i] == 2) {
                    repeatingelement = i;
                    break;
                }
            }
            
            Console.WriteLine("Missing number: " + missingelement);
            Console.WriteLine("Repeated number: " + repeatingelement);
        }
    }
    
    class Program {
        static void Main(string[] args) {
            Arrays a = new Arrays();
            int[] arr = { 0, 1, 1, 3, 4 };
            a.MissingNumberAndRepeatedNumber(arr);
        }
    }
}

The output of the above code is −

Missing number: 2
Repeated number: 1

How It Works

The algorithm processes the input array [0, 1, 1, 3, 4] as follows −

  1. Step 1: Create a boolean array of size arr.Length + 1 initialized to false.

  2. Step 2: Traverse the original array and set tempArray[arr[i]] = true for each element.

  3. Step 3: Find the first index in the boolean array that remains false ? this is the missing number (2).

  4. Step 4: Create an integer array to count occurrences of each element.

  5. Step 5: Find the first element with count 2 ? this is the repeated number (1).

Alternative Optimized Approach

Here's a more efficient solution that finds both numbers in a single pass −

using System;

namespace ConsoleApplication {
    public class OptimizedArrays {
        public void FindMissingAndRepeated(int[] arr) {
            int[] count = new int[arr.Length + 1];
            int missing = -1, repeated = -1;
            
            // Count occurrences and identify repeated number
            for (int i = 0; i < arr.Length; i++) {
                count[arr[i]]++;
                if (count[arr[i]] == 2) {
                    repeated = arr[i];
                }
            }
            
            // Find missing number
            for (int i = 0; i < count.Length; i++) {
                if (count[i] == 0) {
                    missing = i;
                    break;
                }
            }
            
            Console.WriteLine("Missing number: " + missing);
            Console.WriteLine("Repeated number: " + repeated);
        }
    }
    
    class Program {
        static void Main(string[] args) {
            OptimizedArrays opt = new OptimizedArrays();
            int[] arr = { 0, 1, 1, 3, 4 };
            opt.FindMissingAndRepeated(arr);
        }
    }
}

The output of the above code is −

Missing number: 2
Repeated number: 1

Conclusion

This approach efficiently finds both missing and repeated numbers using auxiliary arrays without built-in functions. The time complexity is O(n) and space complexity is O(n), making it suitable for arrays where elements are within a known range.

Updated on: 2026-03-17T07:04:36+05:30

387 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements