--- title: 'Array Using Set' author: 'Hun Im' date: 2024-09-06T11:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- **Using Set** The main difference between using a regular array and a Set is how they handle `duplicate elements`. ```js let setArr = new Set(['a','b','c','d','a']) console.log(setArr) // ์ถ๋ ฅ: Set(4) { 'a', 'b', 'c', 'd' } ``` * A Set is a data structure that does not allow duplicates. * This means that even if you try to add the same value multiple times to a Set, only one instance of that value will be kept. * To convert a Set into an array, you can use `Array.from()`. ```js let report = ["muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "muzi frodo"]; let uniqueReports = new Set(report); console.log(uniqueReports); // ์ถ๋ ฅ: Set(4) { 'muzi frodo', 'apeach frodo', 'frodo neo', 'muzi neo' } ``` **Characteristics of Set** * `Automatically removes duplicates` * It is useful when you need to remove duplicate elements and order is not important.
ย
--- title: 'Set์ ์ฌ์ฉํ๋ ๋ฐฐ์ด' author: '์ํ' date: 2024-09-06T11:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- **Set์ ์ฌ์ฉ** ์ผ๋ฐ ๋ฐฐ์ด์ ์ฌ์ฉํ ๋ฐฉ์์ ์ฐจ์ด๋ `์ค๋ณต๋ ์์๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ๋๊ฐ`์ด๋ค. ```js let setArr = new Set(['a','b','c','d','a']) console.log(setArr) // ์ถ๋ ฅ: Set(4) { 'a', 'b', 'c', 'd' } ``` * Set์ ์ค๋ณต์ ํ์ฉํ์ง ์๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ด๋ค. * ์ฆ, Set์ ๊ฐ์ ๊ฐ์ ์ฌ๋ฌ ๋ฒ ์ถ๊ฐํ๋ ค๊ณ ํด๋, ์ค๋ณต๋ ๊ฐ์ ํ๋๋ง ์ ์ง๋๋ค. * Set์ ๋ฐฐ์ด๋ก ๋ณํํ๋ ค๋ฉด `Array.from()`์ ์ฌ์ฉ ํ ์ ์๋ค. ```js let report = ["muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "muzi frodo"]; let uniqueReports = new Set(report); console.log(uniqueReports); // ์ถ๋ ฅ: Set(4) { 'muzi frodo', 'apeach frodo', 'frodo neo', 'muzi neo' } ``` **Set์ ํน์ง** * `์ค๋ณต์ ์๋์ผ๋ก ์ ๊ฑฐ` * ์์๊ฐ ์ค์ํ ์ํฉ์ด ์๋๋ผ๋ฉด, ์ค๋ณต ์์๋ฅผ ์ ๊ฑฐ ํ ๋ ์ ์ฉํ๋ค.