Array Using Set

--- 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์˜ ํŠน์ง•** * `์ค‘๋ณต์„ ์ž๋™์œผ๋กœ ์ œ๊ฑฐ` * ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•œ ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด, ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.