एक सरणी सेट करें:
int[] arr = {
89,
12,
56,
89,
};
अब, एक नया Dictionary बनाएं:
var d = new Dictionary<int, int>();
डिक्शनरी विधि ContainsKey() का उपयोग करके, सरणी में डुप्लिकेट तत्व खोजें:
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}
यहां पूरा कोड है:
उदाहरण
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = {
89,
12,
56,
89,
};
var d = new Dictionary<int, int>();
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}
foreach(var val in d)
Console.WriteLine("{0} {1} बार आया", val.Key, val.Value);
}
}
}
आउटपुट
89 2 बार आया 12 1 बार आया 56 1 बार आया