Dictionary<int, int> ব্যবহার করে অ্যারেতে প্রতিটি উপাদানের ফ্রিকোয়েন্সি কাউন্ট করা যায়। ContainsKey চেক করে ডুপ্লিকেট শনাক্ত করা হয়।
উদাহরণ
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main() {
int[] arr = { 89, 12, 56, 89 };
var freq = new Dictionary();
foreach (int x in arr) {
if (freq.ContainsKey(x)) freq[x]++;
else freq[x] = 1;
}
foreach (var kvp in freq) {
Console.WriteLine($"{kvp.Key} occurred {kvp.Value} times");
}
}
}
}
আউটপুট
89 occurred 2 times 12 occurred 1 times 56 occurred 1 times