Check if an Array is a permutation of numbers from 1 to N : Set 2 | Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
function permutation ( arr , N ) { var hash = Array ( N + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( var i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " " ; } return " " ; } var arr = [ 1 , 1 , 5 , 5 , 3 ] ; var n = arr . length ; document . write ( permutation ( arr , n ) ) ;