Find original sequence from Array containing the sequence merged many times in order | Java program for the above approach ; 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 ; Driver Code ; Given Array ; Function call
import java . util . * ; class GFG { static Vector < Integer > restore ( int arr [ ] , int N ) { Vector < Integer > result = new Vector < Integer > ( ) ; int count1 = 1 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { s . add ( arr [ i ] ) ; if ( s . size ( ) == count1 ) { result . add ( arr [ i ] ) ; count1 ++ ; } } return result ; } static void print_result ( Vector < Integer > result ) { for ( int i = 0 ; i < result . size ( ) ; i ++ ) System . out . print ( result . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 } ; int N = arr . length ; print_result ( restore ( arr , N ) ) ; } }