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:
- Take any Vector having some elements.
- Declare a LinkedHashMap of the same data type of elements.
- Iterate over Vector using loop and insert the elements into LinkedHashMap along with their frequency.
- After ending of the loop, print the keys of LinkedHashMap. They will be the unique elements preserving the order the same as the vector.
Below is the Program to Extract Unique Elements from a Vector while Preserving the Order using LinkedHashMap:
Java
// 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() + " " ); } } } |
5 3 1 7
Explanation of the Program:
- We have created a
Vector
namedV
and added some elements into it. - Then we have created a
LinkedHashMap
namedLMap
to store unique elements along with their frequency. - It iterates over the
Vector
and inserts each element into theLinkedHashMap and
increments the frequency count if the element already exists. - At last, it iterates over the
LinkedHashMap
and prints the keys, which are the unique elements from theVector
.
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:
- Take any Vector having some elements.
- Declare a LinkedHashSet of same data type of elements.
- Iterate over Vector using loop and insert the elements into LinkedHashSet.
- After ending of loop, print LinkedHashSet. It will contain the unique elements finally, preserving the insertion order.
Below is Program to Extract Unique Elements from a Vector while preserving the Order using LinkedHashSet:
Java
// 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); } } |
Unique elements : [5, 3, 1, 7]
Explanation of the Program:
- It creates a
Vector
namedV
and added some elements into it. - After that it creates a
LinkedHashSet
namedLSet
to store the unique elements while preserving the insertion order. - Then it iterates over the
Vector
and inserts each element into theLinkedHashSet
. - At last, it prints the elements of the
LinkedHashSet
, which are the unique elements from theVector
.
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.