Find original sequence from Array containing the sequence merged many times in order | Function that returns the restored permutation ; Vector to store the result ; Map to mark the elements which are taken in result ; Check if the element is coming first time ; Push in result vector ; Mark it in the map ; Return the answer ; Function to print the result ; Given Array ; Function Call
function restore ( arr , N ) { var result = [ ] ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( ! mp . has ( arr [ i ] ) ) { result . push ( arr [ i ] ) ; mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } } return result ; } function print_result ( result ) { for ( var i = 0 ; i < result . length ; i ++ ) { document . write ( result [ i ] + " " ) ; } } var arr = [ 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 ] ; var N = arr . length ; print_result ( restore ( arr , N ) ) ;