
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
To find the missing number
Create a new array and traverse through the entire array and make the number true in the new array if the number is found Traverse through the entire array and return the first false element as the missing element.
To find the repeating element
The first true element from the new array will be the repeated element.
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; for (int i = 0; i < arr.Length; i++){ int index = arr[i]; if (!tempArray[index]){ tempArray[index] = true; } }; for (int i = 0; i < tempArray.Length; i++){ if (!tempArray[i]){ missingelement = i; break; } } 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; } }; for (int i = 0; i < tempArray1.Length; i++){ if (tempArray1[i]==2){ repeatingelement = i; break; } } Console.WriteLine(missingelement); Console.WriteLine(repeatingelement); } } class Program{ static void Main(string[] args){ Arrays a = new Arrays(); int[] arr = { 0, 1, 1, 3, 4 }; a.MissingNumberAndRepeatedNumber(arr); Console.ReadLine(); } } }
Output
2 1
- Related Articles
- What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
- Find the only missing number in a sorted array using C++
- How to return the first unique character without using inbuilt functions using C#?
- How to return the index of first unique character without inbuilt functions using C#?
- Python program to count upper and lower case characters without using inbuilt functions
- Count upper and lower case characters without using inbuilt functions in Python program
- How to find the number of times array is rotated in the sorted array by recursion using C#?
- Find the one missing number in range using C++
- Find the number of elements greater than k in a sorted array using C++
- Find the repeating and the missing number using two equations in C++
- How to find the missing number in a given Array from number 1 to n in Java?
- C program to print number series without using any loop
- Java Program to print Number series without using any loop
- Finding square root of a number without using library functions - JavaScript
- Code to find the center of an array without using ES6 functions - JavaScript

Advertisements