Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Remove Duplicate Elements From the Array
Next article icon

How to Remove Duplicate Elements from the Vector in Java?

Last Updated : 11 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 



Next Article
Java Program to Remove Duplicate Elements From the Array
author
mukulsomukesh
Improve
Article Tags :
  • Java
  • Java Programs
  • Technical Scripter
  • Java-Vector
  • Technical Scripter 2020
Practice Tags :
  • Java

Similar Reads

  • How to Remove Duplicate Elements From Java LinkedList?
    Linked List is a part of the Collection in java.util package. LinkedList class is an implementation of the LinkedList data structure it is a linear data structure. In LinkedList due to the dynamical allocation of memory, insertions and deletions are easy processes. For removing duplicates from Examp
    4 min read
  • How to Remove Duplicates from a String in Java?
    Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a
    2 min read
  • Java Program to Remove Duplicate Elements From the Array
    Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted. Example: [GFGTABS] Java // Java Program to Remove Dup
    6 min read
  • How to find duplicate elements in a Stream in Java
    Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27,
    5 min read
  • How to Get First and Last Element From the Vector in Java?
    The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any par
    2 min read
  • How to Remove Duplicates from a String in Java Using Regex ?
    In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J
    2 min read
  • How to Remove Duplicates from a String in Java Using Hashing?
    Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. Using hashing is one effective way to do this. By assigning a unique hash code to each element, hashing enables us to keep track of unique items. This article will exam
    2 min read
  • How to Find the Minimum or Maximum Element from the Vector in Java?
    The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below: Methods: Using Collection.min
    3 min read
  • Java Program to Get the Maximum Element From a Vector
    Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l
    4 min read
  • How to Prevent the Addition of Duplicate Elements to the Java ArrayList?
    Ever wondered how you can make an ArrayList unique? Well, in this article we'll be seeing how to prevent the addition of duplicates into our ArrayList. If an ArrayList have three duplicate elements, but at the end, only the ones which are unique are taken into the ArrayList and the repetitions are n
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences