Longest permutation subsequence in a given array | Function to find the longest permutation subsequence ; Map data structure to count the frequency of each element ; If frequency of element is 0 , then we can not move forward as every element should be present ; Increasing the length by one ; Driver code
function longestPermutation ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } let len = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( ! freq . has ( i ) ) { break ; } len ++ ; } return len ; } let arr = [ 3 , 2 , 1 , 6 , 5 ] ; let n = arr . length ; document . write ( longestPermutation ( arr , n ) ) ;