How to Check any value in Array element is duplicate or not in C#

In this article, I will show you how to make a simple program to check any value is duplicate or not in Array. It will return true if the value exists duplicate in Array else false.


using System;
using System.Collections.Generic;

namespace testconsole

{
    class Program
    {
     static void Main(string[] args)
     {
             Console.WriteLine(checkduplicate(1,2,3,2,2));
            Console.ReadLine();
        }
        public static bool checkduplicate(params int[] A)
        {
            Dictionary<int, int> d = new Dictionary<int, int>();
            foreach (int i in A)
            {
                if (d.ContainsKey(i))
                    return true;
                else
                    d.Add(i, 1);
            }
            return false;
        }

    }

}

Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)