본문 바로가기

코딩 테스트/Codility

Codility MissingInteger JavaScript 풀이

https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/

 

MissingInteger coding task - Learn to Code - Codility

Find the smallest positive integer that does not occur in a given sequence.

app.codility.com

내가 처음 제출한 코드이다.

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    let min = Math.min.apply(null, A);
    let max = Math.max.apply(null, A);
    
    if (min < 1) {
        return 1;
    }
    
    A.filter((item, index) => A.indexOf(item) === index);
    
    A.sort();
    
    let result = 0;
    
    A.some((e, i) => {
        if (e !== i+1) result = i + 1;
    })
    
    return result ? result : max + 1;
}

스코어는 22점을 기록했으며, 정확성과 성능 면에서 모두 틀린 테스트 케이스가 있었다.