In this article , I am going to share three ways by which you can find the Duplicate Integers in an Array and the result also include to display the count of the duplicate intergers.
1) Using Linq.
2) Using Dictionary
3) Using HashSet
Have a look on the following ways and if you have any other way , you can share. 🙂
using System; using System.Linq; namespace Nagarro { class Program { static void Main(string[] args) { int[] arr; // Input Array InputArray(out arr); FindDuplicateLinQ(arr); Console.ReadKey(); } ////// Find Duplicate records and how many times it repeats in array /// using Namespace "System.Linq" for GroupBy /// /// private static void FindDuplicateLinQ(int[] arr) { var duplicates = arr.GroupBy(x => x); foreach (var item in duplicates) { if (item.Count() > 1) Console.WriteLine("Number {0} occurred {1} times.", item.Key, item.Count()); } } ////// Enter array size and values /// /// /// private static void InputArray(out int[] arr) { Console.Write("Enter size of integer array : "); int arrLength = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter values for Array : "); arr = new int[arrLength]; string temparray = null; for (int i = 0; i < arrLength; i++) { arr[i] = Convert.ToInt32(Console.ReadLine()); temparray = temparray + " " + arr[i]; // Input Array } Console.Write("Input array : " + temparray + Environment.NewLine); } } }
using System; using System.Collections.Generic; namespace Nagarro { class Program { static void Main(string[] args) { int[] arr; // Input Array InputArray(out arr); FindDuplicate(arr); Console.ReadKey(); } /// <summary> /// Find Duplicate records and how many times it repeats in array /// using Namespace "System.Collections.Generic" for Dictionary /// </summary> /// <param name="arr"></param> private static void FindDuplicate(int[] arr) { var dict = new Dictionary<int, int>(); foreach (var item in arr) { if (dict.ContainsKey(item)) dict[item]++; else dict[item] = 1; } foreach (var item in dict) { if (item.Value > 1) Console.WriteLine("Number {0} occurred {1} times.", item.Key, item.Value); } } /// <summary> /// Enter array size and values /// </summary> /// <param name="arrLength"></param> /// <param name="arr"></param> private static void InputArray(out int[] arr) { Console.Write("Enter size of integer array : "); int arrLength = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter values for Array : "); arr = new int[arrLength]; string temparray = null; for (int i = 0; i < arrLength; i++) { arr[i] = Convert.ToInt32(Console.ReadLine()); temparray = temparray + " " + arr[i]; // Input Array } Console.Write("Input array : " + temparray + Environment.NewLine); } } }
using System; using System.Collections.Generic; namespace Nagarro { class Program { static void Main(string[] args) { int[] arr; // Input Array InputArray(out arr); FindDuplicate(arr); Console.ReadKey(); } /// <summary> /// Find Duplicate records and how many times it repeats in array /// using Namespace "System.Collections.Generic" for HashSet /// </summary> /// <param name="arr"></param> private static void FindDuplicate(int[] arr) { HashSet<int> hs = new HashSet<int>(); int count = 0; for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length; j++) { if (arr[i] == arr[j]) count++; } if (count > 1 && !hs.Contains(arr[i])) { hs.Add(arr[i]); Console.WriteLine("Number {0} occurred {1} times.", arr[i], count); } count = 0; } } /// <summary> /// Enter array size and values /// </summary> /// <param name="arrLength"></param> /// <param name="arr"></param> private static void InputArray(out int[] arr) { Console.Write("Enter size of integer array : "); int arrLength = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter values for Array : "); arr = new int[arrLength]; string temparray = null; for (int i = 0; i < arrLength; i++) { arr[i] = Convert.ToInt32(Console.ReadLine()); temparray = temparray + " " + arr[i]; // Input Array } Console.Write("Input array : " + temparray + Environment.NewLine); } } }