์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜5] ๊ณตํ†ต์›์†Œ ๊ตฌํ•˜๊ธฐ (Javascript)

์€์ง„ 2021. 6. 25. 13:02

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜5] ๊ณตํ†ต์›์†Œ ๊ตฌํ•˜๊ธฐ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธIdea Sketch

2021-06-25

1. arr.includes() ์‚ฌ์šฉ

for (let x of arr1) {
  if (arr2.includes(x)) res.push(x);
}

2. arr.filter ์‚ฌ์šฉ

arr1.filter(v => arr2.includes(v));

3. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

arr.sort((a, b) => a-b)

 

โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-25

function solution(arr1, arr2) {
  return arr1.filter(v => arr2.includes(v)).sort((a, b) => a-b);
}