고유한 요소를 가져오기 위해 배열과 사전을 설정했습니다.
int[] arr = {
88,
23,
56,
96,
43
};
var d = new Dictionary < int, int > (); 사전 컬렉션을 사용하면 목록의 키와 값을 얻을 수 있습니다.
다음은 주어진 정수 배열의 고유한 요소를 표시하는 코드입니다 -
예시
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = {
88,
23,
56,
96,
43
};
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} time", val.Key, val.Value);
}
}
} 출력
88 occurred 1 time 23 occurred 1 time 56 occurred 1 time 96 occurred 1 time 43 occurred 1 time