Construct MEX array from the given array | Javascript program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ;  ; Size of array ; Function call
var MAXN = 100001 ; function constructMEX ( arr , N ) { var hash = Array ( MAXN ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } var MexOfArr ; for ( var i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } var B = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( var i = 0 ; i < N ; i ++ ) document . write ( B [ i ] + ' ' ) ; } Given array * / var arr = [ 2 , 1 , 5 , 3 ] ; var N = arr . length ; constructMEX ( arr , N ) ;