Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to Remove Duplicate Elements from the Vector in Java?

How to Remove Duplicate Elements from the Vector in Java?

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.

  1. Create vector and add elements in the vector.
  2. Create LinkedHashSet and the vector object is passed to the constructor of LinkedHashSet.
  3. Clear all elements of the vector.
  4. 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);
    }
}


Output

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.

  1. Create vector and add elements in the vector.
  2. Create TreeSet and the vector object is passed to the constructor of TreeSet.
  3. Clear all elements of the vector.
  4. 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);
    }
}


Output

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. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments