반응형
HashSet
고유한 요소 집합을 저장하는 컬렉션입니다. 즉, 중복 값을 허용하지 않습니다.
- 생성
HashSet<int> numbers = new HashSet<int>();
- 요소 추가
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(1); // 1은 추가되지 않음. (중복됨)
- 요소 제거
numbers.Remove(2);
- 개수 확인
int count = numbers.Count;
- 루프 사용
foreach (int number in numbers)
{
}
- 해쉬 삭제
numbers.Clear();
- 합집합, 교집합, 차집합
HashSet<int> otherSet = new HashSet<int> { 2, 3, 4 };
numbers.UnionWith(otherSet); // 다른 집합의 요소를 숫자에 추가합니다
numbers.IntersectWith(otherSet); // 공통 요소만 유지합니다
numbers.ExceptWith(otherSet); // 다른 집합에 있는 요소를 제거합니다
- HashSet 생성자
int[] array = { 1, 2, 3, 1, 4 };
HashSet<int> uniqueNumbers = new HashSet<int>(array);
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# Fruit Ninja 과일 닌자 게임 간단 구현 (0) | 2023.11.10 |
---|---|
유니티 C# Pong Clone 탁구 게임 간단 구현 (0) | 2023.11.10 |
유니티 C# 소인수 분해 코드 간단 구현 Prime factorization (0) | 2023.10.27 |
C# Dictionary 딕셔너리 사용법 및 정렬 예시 간단 구현 (0) | 2023.10.20 |
C# 스택, 큐, 재귀 함수 예시 간단 구현 Stack, Queue, Recursive Function (0) | 2023.10.20 |
C# DFS (깊이 우선 탐색) 알고리즘 예시 간단 구현 (0) | 2023.10.20 |
C# BFS (너비 우선 탐색) 알고리즘 예시 간단 구현 (0) | 2023.10.20 |
C# 그리디 알고리즘 (Greedy 탐욕) 예시 간단 구현 (0) | 2023.10.20 |
댓글