[프로그래머스] 폰켓몬 - JavaScript

이은빈 EUNBIN·2021년 4월 8일
0
post-thumbnail

📌 문제

https://programmers.co.kr/learn/courses/30/lessons/1845



📌 풀이

function solution(nums) {
    var answer = [];
    var max = nums.length / 2;
        
    for(let i = 0; i < nums.length; i++) {
        if(answer.length < max) {
            if(!answer.includes(nums[i])) {
                answer.push(nums[i]);
            }
        }
    }
    
    return answer.length;    
}
const solution = nums => {
    let answer = [];
    const max = nums.length / 2;

    nums.map(num => (
        answer.length < max && !answer.includes(num) ? answer.push(num) : num
    ));
    
    return answer.length;    
}
//다른 사람 풀이
const solution = nums => {
    let answer = [...new Set(nums)],
        limit = nums.length / 2;

    return answer.length > limit ? limit : answer.length;
}

자바스크립트 ES6 Set 객체

profile
Frontend Engineer & Value Creator

0개의 댓글