पूर्णांकों की सूची से डुप्लीकेट प्रिंट करने के लिए, ContainsKey का उपयोग करें।
नीचे, हमने पहले पूर्णांक सेट किए हैं।
int[] arr = {
3,
6,
3,
8,
9,
2,
2
};
फिर डिक्शनरी संग्रह का उपयोग डुप्लीकेट पूर्णांकों की गिनती प्राप्त करने के लिए किया जाता है।
आइए डुप्लीकेट पूर्णांक प्राप्त करने का कोड देखें।
उदाहरण
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = {
3,
6,
3,
8,
9,
2,
2
};
var d = new Dictionary();
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);
}
}
}
आउटपुट
3 occurred 2 times 6 occurred 1 times 8 occurred 1 times 9 occurred 1 times 2 occurred 2 times