Basic Programming C# Examples

C# Program to Count Total Number of duplicate elements in array

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:

  1. The program starts by using the “System” namespace.
  2. The program defines a namespace called “CountDuplicates”.
  3. The program defines a class called “Program”.
  4. The program defines the Main method, which is the entry point of the program.
  5. The program asks the user to enter the number of elements in the array and reads the input as an integer.
  6. The program declares an integer array called “numbers” with the size equal to the number entered by the user.
  7. The program uses a for loop to ask the user to enter each element of the array and reads the input as integers.
  8. The program declares an integer variable called “count” and assigns it a value of 0.
  9. The program uses a for loop to iterate through the elements in the “numbers” array.
  10. The program uses a nested for loop to compare each element with all the other elements in the array.
  11. 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.
  12. The program prints the total number of duplicates to the console.
  13. The program waits for the user to press Enter before closing the console.
  14. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X