정수 목록에서 중복 항목을 인쇄하려면 ContainsKey를 사용하세요.
아래에서 먼저 정수를 설정했습니다.
int[] arr = {
3,
6,
3,
8,
9,
2,
2
}; 그런 다음 Dictionary 컬렉션을 사용하여 중복 정수 수를 얻습니다.
중복 정수를 가져오는 코드를 살펴보겠습니다.
예시
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 < 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);
}
}
} 출력
3 occurred 2 times 6 occurred 1 times 8 occurred 1 times 9 occurred 1 times 2 occurred 2 times