
- 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
What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
There are three methods as written below −
In the first method
Use the formula n(n+1)/2 that counts the number of elements and then need to subtract from the elements in the array.
In the second method
Create a new array and traverse through the entire array and make the number false whichever is found.
In the third method
Use Xor operation. which gives the missing number.
Example
using System; namespace ConsoleApplication{ public class Arrays{ public int MissingNumber1(int[] arr){ int totalcount = 0; for (int i = 0; i < arr.Length; i++){ totalcount += arr[i]; } int count = (arr.Length * (arr.Length + 1)) / 2; return count - totalcount; } public int MissingNumber2(int[] arr){ bool[] tempArray = new bool[arr.Length + 1]; int element = -1; for (int i = 0; i < arr.Length; i++){ int index = arr[i]; tempArray[index] = true; } for (int i = 0; i < tempArray.Length; i++){ if (tempArray[i] == false){ element = i; break; } } return element; } public int MissingNumber3(int[] arr){ int result = 1; for (int i = 0; i < arr.Length; i++){ result = result ^ arr[i]; } return result; } } class Program{ static void Main(string[] args){ Arrays a = new Arrays(); int[] arr = { 0, 1, 3, 4, 5 }; Console.WriteLine(a.MissingNumber1(arr)); Console.WriteLine(a.MissingNumber2(arr)); Console.WriteLine(a.MissingNumber3(arr)); Console.ReadLine(); } } }
Output
2 2 2
- Related Articles
- How to find the missing number and the repeated number in a sorted array without using 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#?
- Find missing element in a sorted array of consecutive numbers in Python
- Find missing element in a sorted array of consecutive numbers in C++
- What are different ways of defining functions in JavaScript?
- How to return the index of first unique character without inbuilt functions using C#?
- Find missing numbers in a sorted list range in Python
- 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
- PHP program to find the numbers within a given array that are missing
- What are the different ways to iterate over an array in Java?
- Code to find the center of an array without using ES6 functions - JavaScript
- PHP program to find the first ‘n’ numbers that are missing in an array
- Missing Element in Sorted Array in C++

Advertisements