Largest subset with M as smallest missing number | Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver code
function findLengthOfMaxSubset ( arr , n , m ) { let s = new Set ( ) ; let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let tmp = arr [ i ] ; if ( tmp != m ) { s . add ( tmp ) ; answer ++ ; } } let min = 1 ; while ( s . has ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } let arr = [ 1 , 2 , 4 ] ; let N = arr . length ; let M = 3 ; document . write ( findLengthOfMaxSubset ( arr , N , M ) ) ;