먼저 중복 요소가 있는 배열을 설정합니다.
int[] arr = {
24,
10,
56,
32,
10,
43,
88,
32
}; 이제 Dictionary를 선언하고 배열을 반복하여 반복되는 요소를 가져옵니다.
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 = {
24,
10,
56,
32,
10,
43,
88,
32
};
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);
}
}
} 출력
24 occurred 1 times 10 occurred 2 times 56 occurred 1 times 32 occurred 2 times 43 occurred 1 times 88 occurred 1 times