Find first k natural numbers missing in given array | Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code
function printmissingk ( arr , n , k ) { let d = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) d . set ( arr [ i ] , arr [ i ] ) ; let cnt = 1 ; let fl = 0 ; for ( let i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . has ( cnt ) ) { fl += 1 ; document . write ( cnt + " " ) ; if ( fl == k ) break ; } cnt += 1 ; } } let arr = [ 1 , 4 , 3 ] ; let n = arr . length ; let k = 3 ; printmissingk ( arr , n , k ) ;