Open In App

Extract Unique Elements from a Vector While Preserving the Order in Java

Vector in Java can be understood as a dynamic size array, that can be shrink and grow as per the requirement. Vector can be assumed similar to ArrayList, but Vector contains some of the extra methods as compared to it. Usually, a vector is not used much because of its poor performance of insertion, deletion and update operations with elements synchronized nature.

In this article, we will learn how we can extract unique elements from vectors.



Methods to Extract Unique Elements while Preserving Order

Below are the methods to Extract Unique Elements from a Vector.

Method 1 (Using LinkedHashMap):

LinkedHashMap in Java is a modified map that considers the insertion order of elements along with their frequencies. So, LinkedHashMap can be used to store elements of vectors on iterating over it.



Implementation Steps:

Below is the Program to Extract Unique Elements from a Vector while Preserving the Order using LinkedHashMap:




// Java Program to obtain unique elements from Vector
// Preserving the insertion order using LinkedHashMap
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        // Vector object
        Vector<Integer> V = new Vector<>();
  
        // Adding elements into Vector
        V.add(5);
        V.add(3);
        V.add(1);
        V.add(5);
        V.add(7);
  
        // LinkedHashMap to store the unique elements along
        // With their frequency
        LinkedHashMap<Integer, Integer> LMap
            = new LinkedHashMap<>();
  
        // Iterating over Vector and inserting elements into
        // LinkedHashMap
        for (int element : V) {
            LMap.put(element, LMap.get(element) == null
                                  ? 1
                                  : LMap.get(element) + 1);
        }
  
        // Iterating over LinkedHashMap and printing keys
        for (Map.Entry<Integer, Integer> set :
             LMap.entrySet()) {
            System.out.print(set.getKey() + " ");
        }
    }
}

Output
5 3 1 7 


Explanation of the Program:

Method 2 (Using LinkedHashSet):

LinkedHashSet in Java is a modified Set, which considers the insertion of only unique elements while preserving the insertion order of elements. So, LinkedHashSet can be used to store elements of vector on iterating over it.

Implementation Steps:

Below is Program to Extract Unique Elements from a Vector while preserving the Order using LinkedHashSet:




// Java Program to obtain unique elements from Vector
// Preserving the insertion order using LinkedHashSet
import java.util.*;
  
// Driver Class
public class Main {
      // Main Function
    public static void main(String[] args)
    {
        // Vector object
        Vector<Integer> V = new Vector<>();
  
        // Adding elements into Vector
        V.add(5);
        V.add(3);
        V.add(1);
        V.add(5);
        V.add(7);
  
        // LinkedHashSet to store the unique elements
        LinkedHashSet<Integer> LSet = new LinkedHashSet<>();
  
        // Iterating over Vector and inserting elements into
        // LinkedHashSet
        for (int element : V) {
            LSet.add(element);
        }
  
        // Printing elements of LinkedHashSet
        System.out.println("Unique elements : " + LSet);
    }
}

Output
Unique elements : [5, 3, 1, 7]



Explanation of the Program:


Article Tags :