Extract Unique Elements from a Vector While Preserving the Order in Java
Last Updated :
21 Feb, 2024
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:
- 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
import java.util.*;
public class Main {
public static void main(String[] args)
{
Vector<Integer> V = new Vector<>();
V.add( 5 );
V.add( 3 );
V.add( 1 );
V.add( 5 );
V.add( 7 );
LinkedHashMap<Integer, Integer> LMap
= new LinkedHashMap<>();
for ( int element : V) {
LMap.put(element, LMap.get(element) == null
? 1
: LMap.get(element) + 1 );
}
for (Map.Entry<Integer, Integer> set :
LMap.entrySet()) {
System.out.print(set.getKey() + " " );
}
}
}
|
Explanation of the Program:
- We have created a
Vector
named V
and added some elements into it.
- Then we have created a
LinkedHashMap
named LMap
to store unique elements along with their frequency.
- It iterates over the
Vector
and inserts each element into the LinkedHashMap 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 the Vector
.
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
import java.util.*;
public class Main {
public static void main(String[] args)
{
Vector<Integer> V = new Vector<>();
V.add( 5 );
V.add( 3 );
V.add( 1 );
V.add( 5 );
V.add( 7 );
LinkedHashSet<Integer> LSet = new LinkedHashSet<>();
for ( int element : V) {
LSet.add(element);
}
System.out.println( "Unique elements : " + LSet);
}
}
|
Output
Unique elements : [5, 3, 1, 7]
Explanation of the Program:
- It creates a
Vector
named V
and added some elements into it.
- After that it creates a
LinkedHashSet
named LSet
to store the unique elements while preserving the insertion order.
- Then it iterates over the
Vector
and inserts each element into the LinkedHashSet
.
- At last, it prints the elements of the
LinkedHashSet
, which are the unique elements from the Vector
.
Share your thoughts in the comments
Please Login to comment...