Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 664 Bytes

PermCheck.md

File metadata and controls

22 lines (17 loc) · 664 Bytes

Check whether array A is a permutation.

Solution (JavaScript)

This problem can easily be solved using a second array to track the found numbers and their occurance. We can then loop through and exit if a number is missing or occurs more than once.

Test Score: 100%

function solution(A) {
    let B = []
    for (let i = 0; i < A.length; ++i) {
        B[A[i]] = ( B[A[i]] || 0 ) + 1    
    }
    
    for (let i = 1; i <= A.length; ++i) {
        if ( B[i] !== 1 ) return 0
    }
    
    return 1
}