एक सरणी सेट करें -
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;
} यहाँ पूरा कोड है -
उदाहरण
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} occurred {1} times", val.Key, val.Value);
}
}
} आउटपुट
89 occurred 2 times 12 occurred 1 times 56 occurred 1 times