1. 중복 제거: Set을 활용한 배열 중복 제거

→ **Set**는 중복을 허용하지 않는 자료구조로, 배열을 Set으로 변환하면 중복이 자동으로 제거됨.

예제코드

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // [1, 2, 3, 4, 5]

연습문제 풀어보기!!

문제. 문자열 배열에서 중복된 단어를 제거하기.

const words = ["apple", "banana", "apple", "orange", "banana"];
// 중복 제거 코드 작성
const uniqueWords = [...new Set(words)];
console.log(uniqueWords); // ["apple", "banana", "orange"];

2. 최대값, 최소값 찾기

2.1. Math.maxMath.min 활용

Math.maxMath.min은 숫자 배열의 최대값과 최소값을 찾는 데 유용함. 배열을 spread operator[…]를 사용해 넘겨야 함.

예제코드

const numbers = [1, 5, 2, 9, 3];
const max = Math.max(...numbers);
const min = Math.min(...numbers);
console.log(`Max: ${max}, Min: ${min}`); // Max: 9, Min: 1

2.2. reduce 활용

예제코드

reduce는 배열의 각 요소를 순회하면서 **누적 값(acc)**을 업데이트하는 메서드. 여기선 acc가 최대값 또는 최소값을 저장하는 역할.