How to get duplicate value from the array in C#

How to get duplicate value from the array in C#


Solution:-

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

namespace LogicTest
{
    class Program
    {
        static void Main(string[] args)
        {
         
            int[] arr = new int[] { 1,2,3,1,4};
            int count = 0;
            var num = 0;
            for (int i = 0; i < arr.Length; i++)
            {

                for (int j = i; j < arr.Length-1 ; j++)
                {
                    if (arr[i] == arr[j+1])
                    {
                     
                        Console.WriteLine("duplicate  "+arr[i]);
                        count++;
                        break;
                    }
                }
            }
            Console.WriteLine("duplicate elements found in the array is: {0} {1}", count, num);
            Console.ReadLine();

        }
    }

Output:- duplicate  1
duplicate elements found in the array is: 1 0

Share this

Previous
Next Post »