|
| 1 | +## 1. Set(집합) |
| 2 | +- 중복을 허용하지 않는 값들의 모음을 표현하는 자료구조, |
| 3 | +- 수학의 집합 개념을 구현한 자료구조 |
| 4 | + |
| 5 | +## 2. Set 구현 |
| 6 | + |
| 7 | +### 2.1 기본 생성 |
| 8 | +```javascript |
| 9 | +// Set 생성 |
| 10 | +const set = new Set(); |
| 11 | +const setFromArray = new Set([1, 2, 3]); |
| 12 | + |
| 13 | +// 값 추가/삭제 |
| 14 | +set.add(4); // 값 추가 |
| 15 | +set.delete(4); // 값 삭제 |
| 16 | +set.clear(); // 모든 값 삭제 |
| 17 | + |
| 18 | +// 값 확인 |
| 19 | +set.has(4); // 값 존재 여부 |
| 20 | +set.size; // Set 크기 |
| 21 | +``` |
| 22 | + |
| 23 | +### 2.2 기본 연산 |
| 24 | +```javascript |
| 25 | +class CustomSet { |
| 26 | + constructor() { |
| 27 | + this.items = {}; |
| 28 | + } |
| 29 | + |
| 30 | + // 원소 추가 |
| 31 | + add(element) { |
| 32 | + if (!this.has(element)) { |
| 33 | + this.items[element] = element; |
| 34 | + return true; |
| 35 | + } |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + // 원소 삭제 |
| 40 | + delete(element) { |
| 41 | + if (this.has(element)) { |
| 42 | + delete this.items[element]; |
| 43 | + return true; |
| 44 | + } |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + // 원소 확인 |
| 49 | + has(element) { |
| 50 | + return element in this.items; |
| 51 | + } |
| 52 | + |
| 53 | + // 모든 원소 반환 |
| 54 | + values() { |
| 55 | + return Object.values(this.items); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## 3. Set 연산 메서드 |
| 61 | + |
| 62 | +### 3.1 합집합(Union) |
| 63 | +```javascript |
| 64 | +function union(setA, setB) { |
| 65 | + const unionSet = new Set(setA); |
| 66 | + for (const element of setB) { |
| 67 | + unionSet.add(element); |
| 68 | + } |
| 69 | + return unionSet; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### 3.2 교집합(Intersection) |
| 74 | +```javascript |
| 75 | +function intersection(setA, setB) { |
| 76 | + const intersectionSet = new Set(); |
| 77 | + for (const element of setA) { |
| 78 | + if (setB.has(element)) { |
| 79 | + intersectionSet.add(element); |
| 80 | + } |
| 81 | + } |
| 82 | + return intersectionSet; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### 3.3 차집합(Difference) |
| 87 | +```javascript |
| 88 | +function difference(setA, setB) { |
| 89 | + const differenceSet = new Set(setA); |
| 90 | + for (const element of setB) { |
| 91 | + differenceSet.delete(element); |
| 92 | + } |
| 93 | + return differenceSet; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### 3.4 부분집합(Subset) 확인 |
| 98 | +```javascript |
| 99 | +function isSubset(setA, setB) { |
| 100 | + for (const element of setA) { |
| 101 | + if (!setB.has(element)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + return true; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## 4. Set 활용 |
| 110 | + |
| 111 | +### 4.1 배열 중복 제거 |
| 112 | +```javascript |
| 113 | +function removeDuplicates(array) { |
| 114 | + return [...new Set(array)]; |
| 115 | +} |
| 116 | + |
| 117 | +// 예시 |
| 118 | +const array = [1, 2, 2, 3, 3, 4]; |
| 119 | +console.log(removeDuplicates(array)); // [1, 2, 3, 4] |
| 120 | +``` |
| 121 | + |
| 122 | +### 4.2 문자열 중복 문자 제거 |
| 123 | +```javascript |
| 124 | +function removeDuplicateChars(str) { |
| 125 | + return [...new Set(str)].join(''); |
| 126 | +} |
| 127 | + |
| 128 | +// 예시 |
| 129 | +console.log(removeDuplicateChars('hello')); // 'helo' |
| 130 | +``` |
| 131 | + |
| 132 | +## 5. 성능 고려사항 |
| 133 | + |
| 134 | +### 5.1 시간 복잡도 |
| 135 | +- 추가(add): O(1) |
| 136 | +- 삭제(delete): O(1) |
| 137 | +- 검색(has): O(1) |
| 138 | +- 크기 확인(size): O(1) |
| 139 | + |
| 140 | +### 5.2 공간 복잡도 |
| 141 | +- O(n), n은 저장된 원소의 수 |
0 commit comments