Question #294394

In C# please.

Create a program that will accept inputs into 25-element one-dimensional integer

array IntArray. Your program should count the duplicate numbers appeared in the list of

accepted values. Moreover, you are also tasked to display the duplicate numbers that

appeared in the list.


Expert's answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr= new int[25];
            int count = 0;
            List<int> duplicateNumbers = new List<int>();

            Random r = new Random();
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("Input {0} number :", i + 1);
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = i+1; j < arr.Length; j++)
                {
                    if (arr[i] == arr[j])
                    {
                        if (!duplicateNumbers.Contains(arr[i]))
                        {
                            duplicateNumbers.Add(arr[i]);
                            count++;
                        }
                    }
                }
            }

            Console.WriteLine("Duplicate count: {0}", count);
            Console.WriteLine("Duplicate numbers:");

            for (int i = 0; i < duplicateNumbers.Count; i++)
            {
                Console.WriteLine(duplicateNumbers[i]);
                
            }
            Console.ReadKey();
        }
    }
}

LATEST TUTORIALS
APPROVED BY CLIENTS