Find original sequence from Array containing the sequence merged many times in order | Function that returns the restored permutation ; Vector to store the result ; Set to insert unique elements ; Check if the element is coming first time ; Push in result vector ; Function to print the result ; Given Array ; Function call
function restore ( arr , N ) { let result = [ ] ; let count1 = 1 ; let s = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { s . add ( arr [ i ] ) ; if ( s . size == count1 ) { result . push ( arr [ i ] ) ; count1 ++ ; } } return result ; } function print_result ( result ) { for ( let i = 0 ; i < result . length ; i ++ ) document . write ( result [ i ] + " " ) ; } let arr = [ 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 ] ; let N = arr . length ; print_result ( restore ( arr , N ) ) ;