๋ฐ˜์‘ํ˜•

๋ฐฉ๋ฒ• 1 — Set ์ด์šฉ โญ๏ธ


Set์„ ์ด์šฉํ•ด ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์˜ ์„ฑ๋Šฅ์ด ๊ฐ€์žฅ ์ข‹๋‹ค

 

Set์€ ์œ ๋‹ˆํฌํ•œ ๊ฐ’๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— new Set() ์ธ์ž์— ๋“ค์–ด์˜จ ๋ฐฐ์—ด์˜ ์ค‘๋ณต ์š”์†Œ๋Š” ์ž๋™์œผ๋กœ ์ œ๊ฑฐ๋œ๋‹ค. Array.from์€ โžŠ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด๋‚˜ โž‹๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์–•๊ฒŒ ๋ณต์‚ฌํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ „๊ฐœ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ๊ฒฐ๊ณผ๋Š” ๋™์ผ.

const arr = ["A", "B", "A", "D", "B", "F"];
const deDuplicated = [...new Set(arr)];
// const deDuplicated = Array.from(new Set(arr))
console.log(deDuplicated); // [ 'A', 'B', 'D', 'F' ]

 

Set, Map ๊ด€๋ จ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Map / Set ์ž๋ฃŒ๊ตฌ์กฐ ๋…ธํŠธ ์ฐธ๊ณ .

 

  • new Set([iterable]) : ์ƒˆ๋กœ์šด Set ์ƒ์„ฑ
  • set.add(value) : value ์ถ”๊ฐ€ ํ›„ Set ๋ฐ˜ํ™˜. ๋งŒ์•ฝ ์ธ์ž์— ์ž…๋ ฅํ•œ value๊ฐ€ ๊ธฐ์กด Set ์•ˆ์— ์žˆ์œผ๋ฉด ์•„๋ฌด๋Ÿฐ ์ž‘์—… ์•ˆํ•จ
  • set.delete(value) : ๊ฐ’ ์ œ๊ฑฐํ›„, ์ œ๊ฑฐ ์„ฑ๊ณต ์—ฌ๋ถ€ true / false ๋ฐ˜ํ™˜
  • set.has(value) : ์ธ์ž์— ์ž…๋ ฅํ•œ value๊ฐ€ Set์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ. Boolean ๊ฐ’ ๋ฐ˜ํ™˜
  • set.clear() : Set ์ดˆ๊ธฐํ™”
  • set.size : Set์— ๋ช‡๊ฐœ์˜ value๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ

 

๋ฐฉ๋ฒ• 2 — filter / indexOf ์ด์šฉ


์ค‘๋ณต๊ฐ’๋งŒ ๋”ฐ๋กœ ๋ฐฐ์—ด์— ๋‹ด๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉ

 

arr.indexOf() ๋ฉ”์„œ๋“œ๋Š” ์ธ์ž์— ์ž…๋ ฅํ•œ ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ์ž์— ์ž…๋ ฅํ•œ ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์–ด๋„ ์ฒซ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—†์œผ๋ฉฐ -1 ๋ฐ˜ํ™˜.

 

filter ๋ฉ”์†Œ๋“œ๋กœ arr ๋ฐฐ์—ด์˜ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” A ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ์˜ ์ธ๋ฑ์Šค๋Š” 2์ง€๋งŒ indexOf๋กœ ์ฐพ์€ ์ธ๋ฑ์Šค๋Š” 0์ด๋ฏ€๋กœ false ๋ฐ˜ํ™˜. ์ด ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

const arr = ["A", "B", "A", "D", "B", "F"];
const deDuplicated = arr.filter((el, idx) => arr.indexOf(el) === idx);
// ์ค‘๋ณต๊ฐ’๋งŒ ๋ฐฐ์—ด์— ๋‹ด๊ณ  ์‹ถ๋‹ค๋ฉด arr.indexOf(el) !== idx
console.log(deDuplicated); // [ 'A', 'B', 'D', 'F' ]

 

๋ฐฉ๋ฒ• 3 — forEach / includes ์‚ฌ์šฉ


includes๋Š” ์ธ์ž์— ์ž…๋ ฅํ•œ ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ ํ›„ Boolean ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•ด for๋ฌธ์ด๋‚˜ forEach ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

const arr = ["A", "B", "A", "D", "B", "F"];
const deDuplicated = [];
arr.forEach((el) => {
  if (deDuplicated.includes(el) === false) {
    deDuplicated.push(el);
  }
});
console.log(deDuplicated); // [ 'A', 'B', 'D', 'F' ]

 

๋ฐฉ๋ฒ• 4 — reduce / includes ์‚ฌ์šฉ


`includes`, `indexOf`๋Š” ๋ฐฐ์—ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ ๋ฌธ์ž์—ด๋„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

reduce์—์„œ ์ดˆ๊ธฐ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฒซ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ๋ˆ„์ ๊ฐ’(acc)๊ฐ€ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ 'A'๊ฐ€ unique(๋ˆ„์ ๊ฐ’) ๊ฐ’์ด ๋œ๋‹ค. 2๋ฒˆ์งธ ์ˆœํšŒ์—์„  if ๋ฌธ์„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๊ณ  ['A', 'B']๊ฐ€ unique ๊ฐ’์ด ๋œ๋‹ค. 3๋ฒˆ์งธ ์ˆœํšŒ์—์„  unique์— ์ˆœํšŒํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ if๋ฌธ ์กฐ๊ฑด ํ†ต๊ณผํ•˜์—ฌ ๋ˆ„์ ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ด. ๋‹ค์Œ ์š”์†Œ ์ˆœํšŒํ•˜๋ฉฐ ๋ฐ˜๋ณต...

const arr = ["A", "B", "A", "D", "B", "F"];
const deDuplicated = arr.reduce((unique, item) => {
  if (unique.includes(item)) {
    return unique;
  }
  return [...unique, item];
});
console.log(deDuplicated); // [ 'A', 'B', 'D', 'F' ]

 

console.time ํ…Œ์ŠคํŠธ


`console.time('ํƒ€์ด๋จธ์ด๋ฆ„')`, `console.timeEnd('ํƒ€์ด๋จธ์ด๋ฆ„')`๋กœ ์ฝ”๋“œ ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ์—” ํƒ€์ด๋จธ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๋ฉฐ, time๊ณผ timeEnd ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

 

Set ์‚ฌ์šฉ ์‹œ : 0.0073ms
Filter ์‚ฌ์šฉ ์‹œ : 0.03ms
forEach ์‚ฌ์šฉ ์‹œ : 0.05ms
Reduce ์‚ฌ์šฉ ์‹œ : 0.04ms

 

๋ ˆํผ๋Ÿฐ์Šค


How to Remove Array Duplicates in ES6  

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•