ㅇㅇㅈ Blog

프론트엔드 수행중

0%

중복단어제거

중복 단어 제거

n개의 문자열이 입력되면 중복된 문자열은 제거하고 출력한다

1
2
3
4
5
6
7
8
9
10
let textArr = ['5', 'good', 'time', 'good', 'student', '5', 'hello', 'bye']

function solution(t) {
const ans = t.filter((text, index) => {
return textArr.indexOf(text) === index
})
console.log(ans)
}

solution(textArr)

indexOf()

  • 배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환한다.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

filter((element, index, source) => {})

  • 주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환한다.

element : 요소값
index(optional) : 요소의 인덱스
source(optional) : 순회되는 배열 객체

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter