The Program in C# Program to Count Total Number of duplicate elements in array is given below:
using System;
namespace CountDuplicates
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter an element below to check the number of duplicates ");
Console.WriteLine("Enter the number of elements in the array: ");
int n = int.Parse(Console.ReadLine());
int[] numbers = new int[n];
Console.WriteLine("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
}
int count = 0;
for (int i = 0; i < numbers.Length; i++)
{
for (int j = i + 1; j < numbers.Length; j++)
{
if (numbers[i] == numbers[j])
{
count++;
break;
}
}
}
Console.WriteLine("Okay, the total number of duplicates: " + count);
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family,enter an element below to check the number of duplicates
Enter the number of elements in the array: 5
Enter the elements of the array:
10
20
10
40
44
Okay, the total number of duplicates: 1
Pro-Tips💡
Here are the step by step execution of above program:
- The program starts by using the “System” namespace.
- The program defines a namespace called “CountDuplicates”.
- The program defines a class called “Program”.
- The program defines the Main method, which is the entry point of the program.
- The program asks the user to enter the number of elements in the array and reads the input as an integer.
- The program declares an integer array called “numbers” with the size equal to the number entered by the user.
- The program uses a for loop to ask the user to enter each element of the array and reads the input as integers.
- The program declares an integer variable called “count” and assigns it a value of 0.
- The program uses a for loop to iterate through the elements in the “numbers” array.
- The program uses a nested for loop to compare each element with all the other elements in the array.
- The program checks if the current element is equal to the current element in the nested loop. If they are equal, the program increments the “count” variable.
- The program prints the total number of duplicates to the console.
- The program waits for the user to press Enter before closing the console.
- The program ends by returning 0 from the Main method.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.