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
Post a Comment