Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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 −
-
Step 1: Create a boolean array of size
arr.Length + 1initialized to false. -
Step 2: Traverse the original array and set
tempArray[arr[i]] = truefor each element. -
Step 3: Find the first index in the boolean array that remains false ? this is the missing number (2).
-
Step 4: Create an integer array to count occurrences of each element.
-
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.
