Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements.
Example:
Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]
Approach 1: Using LinkedHashSet
LinkedHashSet does not accept duplicate elements and also not maintains sorted order.
- Create vector and add elements in the vector.
- Create LinkedHashSet and the vector object is passed to the constructor of LinkedHashSet.
- Clear all elements of the vector.
- Add all elements of LinkedHashSet in vector.
Below is the implementation of the above approach:
Java
// Java Program to remove duplicate // elements from Vector import java.util.LinkedHashSet; import java.util.Vector; public class GFG { public static void main(String[] args) { Vector<Integer> vector = new Vector<Integer>(); vector.add( 2 ); vector.add( 2 ); vector.add( 4 ); vector.add( 2 ); vector.add( 3 ); vector.add( 2 ); vector.add( 1 ); // display original elements System.out.println( "Display original Vector - " + vector); // convert Vector to a LinkedHashSet object. LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(vector); // clear all elements of vector vector.clear(); // add all unique elements LinkedHashSet to the // vector vector.addAll(hashSet); // display vector after removing duplicate elements System.out.println( "After removing duplicate elements - " + vector); } } |
Display original Vector - [2, 2, 4, 2, 3, 2, 1] After removing duplicate elements - [2, 4, 3, 1]
Time Complexity: O(N), where N is the length of the original Vector.
Approach 2: TreeSet
The TreeSet does not accept duplicate elements and TreeSet maintains sorted order.
- Create vector and add elements in the vector.
- Create TreeSet and the vector object is passed to the constructor of TreeSet.
- Clear all elements of the vector.
- Add all elements of TreeSet in vector.
Below is the implementation of the above approach:
Java
// Java Program to remove duplicate // elements from Vector import java.util.TreeSet; import java.util.Vector; public class GFG { public static void main(String[] args) { // create vector Vector<Integer> vector = new Vector<Integer>(); // add elements in vector vector.add( 4 ); vector.add( 2 ); vector.add( 3 ); vector.add( 1 ); vector.add( 3 ); vector.add( 2 ); vector.add( 4 ); // display original vector System.out.println( "Display original Vector - " + vector); // convert Vector to a TreeSet object. TreeSet<Integer> treeSet = new TreeSet<Integer>(vector); // clear all elements of vector vector.clear(); // add all unique elements of TreeSet to the vector vector.addAll(treeSet); // display vector after removing duplicate elements System.out.println( "After removing duplicate elements - " + vector); } } |
Display original Vector - [4, 2, 3, 1, 3, 2, 4] After removing duplicate elements - [1, 2, 3, 4]
Time Complexity: O(n log n), Because TreeSet uses RedBlack tree implementation.