A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write C# program to count total duplicate elements in an array
Q:

Write C# program to count total duplicate elements in an array

0

Write C# program to count total duplicate elements in an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
class Program
{
    static void Main()
    {       
        int[] arr = new int[100];
        int i, j, num, count = 0;
 
        //Reads size of the array
        Console.WriteLine("Enter size of the array: ");
        num = Convert.ToInt32(Console.ReadLine());
 
        //Reads elements in array
        Console.WriteLine("Enter elements in the array: ");
        for (i = 0; i < num; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());           
        }
 
        //Find all duplicate elements in array
        for (i = 0; i < num; i++)
        {
            for (j = i + 1; j < num; j++)
            {
                // If duplicate element found then increment count by 1
                if (arr[i] == arr[j])
                {
                    count++;
                    break;
                }
            }
        }
 
        Console.WriteLine("\n Total number of duplicate elements found in array:"+count);
        Console.ReadLine();
    }
 
}

Result:

Enter size of the array: 

5

Enter elements in the array: 

10

20

30

20

30

Total number of duplicate elements found in array:2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now