2

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

 3 weeks ago
source link: https://www.geeksforgeeks.org/extract-unique-elements-from-a-vector-while-preserving-the-order-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 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:

  • We have created a Vector named Vand 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 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:

  • It creates a Vector namedVand added some elements into it.
  • After that it creates a LinkedHashSet named LSetto 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.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks
Like Article
Suggest improvement
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK