Problem Statement
We have a sequence of integers, and we would like to remove all duplicate elements from this sequence. There may be multiple ways to perform this task. For example, given the sequence { 1, 2, 1, 3 }, we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence. For this problem, we want to return the lexicographically first of of all possible remaining sequences. A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ (so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
Definition
- Class:
- HardDuplicateRemover
- Method:
- process
- Parameters:
- int[]
- Returns:
- int[]
- Method signature:
- int[] process(int[] sequence)
- (be sure your method is public)
Constraints
- sequence will have between 1 and 50 elements, inclusive.
- Each element of sequence will be between 1 and 1000, inclusive.
Examples
{5, 6, 5, 1, 6, 5}Returns: {1, 6, 5 }There are six different ways to remove duplicates (remaining numbers are marked by '*'): { *5, *6, 5, *1, 6, 5}, { *5, 6, 5, *1, *6, 5}, { 5, *6, *5, *1, 6, 5}, { 5, 6, *5, *1, *6, 5}, { 5, *6, 5, *1, 6, *5}, { 5, 6, 5, *1, *6, *5}. The last variant is the lexicographically first.{3, 2, 4, 2, 4, 4}Returns: {3, 2, 4 }{6, 6, 6, 6, 6, 6}Returns: {6 }{1, 3, 2, 4, 2, 3}Returns: {1, 2, 4, 3 }{5, 4, 1, 5}Returns: {4, 1, 5 }