Java Program to Remove Duplicate Entries from an Array using TreeSet
Last Updated :
20 Apr, 2021
Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows:
- TreeSet implements the SortedSet interface. So, duplicate values are not allowed and will be leftovers.
- Objects in a TreeSet are stored in a sorted and ascending order.
- TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
- If we are depending on the default natural sorting order, the objects that are being inserted into the tree should be homogeneous and comparable. TreeSet does not allow the inserting of Heterogeneous objects. It will throw a classCastException at Runtime if we try to add heterogeneous objects.
Approach: add() method of Set
In Java, here it is used in context to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set.
Syntax:
boolean add(E element)
Where, E is the type of element maintained
by this Set collection.
Procedure:
- Create an object of TreeSet.
- Add array elements to the TreeSet using the add() method.
- Print and display elements in an array that are duplicated using add(element) method.
- Print and display elements in an array after removing duplicates from it.
- Getting size of TreeSet using size() method.
- Converting above TreeSet to arrays using toArray() method for conversion.
- Iterating over array elements.
- Print and display the above string array after removing duplicate entries from it.
Implementation:
In the below example we are Addition for every element of the Array to the TreeSet later on checking for duplicates. Finally, putting all the elements of TreeSet in Array and put “null” for all leftover spaces in Array, and printing the elements of the array.
Example
Java
// Java Program to remove duplicates entries
// from an Array using TreeSet
// Importing Arrays and TreeSet class from
// java.util package
import java.util.Arrays;
import java.util.TreeSet;
// Class to remove duplicates
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Input custom entries in an array
// String type
// Custom inputs
String[] input
= new String[] { "Hello", "hi", "Wow",
"cute", "thanks", "hi",
"Aww", "cute", "baby",
"beloved", "Aww" };
// Converting Array to String and printing it
System.out.print(
"Initial String Array(Containing Duplicates) : "
+ (Arrays.toString(input)));
// Creating an object of TreeSet
TreeSet<String> dupliCheckr = new TreeSet<String>();
// Adding array elements in TreeSet
// For added elements in TreeSet
for (String element : input) {
// Displaying duplicate entries
if (!dupliCheckr.add(element)) {
// Print and display elements in an array
// which are duplicated.
System.out.println(
"Duplicate Data entered : " + element);
}
}
// Next line
System.out.println();
// Print and display elements in an array
// after removing duplicates from it.
System.out.println(
"TreeSet(After Removing Duplicates) : "
+ dupliCheckr);
// Next line
System.out.println();
// Getting size of TreeSet using size() method
int length = dupliCheckr.size();
// Converting above TreeSet to arrays
// Using toArray() method
input = dupliCheckr.toArray(input);
// Iterating over array elements
for (int i = length; i < input.length; i++)
input[i] = null;
// Print and display above string array
// after removing duplicate entries from it
System.out.println("Final String Array is : "
+ Arrays.toString(input));
}
}
OutputInitial String Array(Containing Duplicates) : [Hello, hi, Wow, cute, thanks, hi, Aww, cute, baby, beloved, Aww]Duplicate Data entered : hi
Duplicate Data entered : cute
Duplicate Data entered : Aww
TreeSet(After Removing Duplicates) : [Aww, Hello, Wow, baby, beloved, cute, hi, thanks]
Final String Array is : [Aww, Hello, Wow, baby, beloved, cute, hi, thanks, null, null, null]
Similar Reads
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:Java// Java Program to Remove Duplicate // Ele
6 min read
Java Program To Remove All The Duplicate Entries From The Collection As we know that the HashSet contains only unique elements, ie no duplicate entries are allowed, and since our aim is to remove the duplicate entries from the collection, so for removing all the duplicate entries from the collection, we will use HashSet.The HashSet class implements the Set interface,
3 min read
How to Efficiently Remove Duplicates from an Array without using Set? Arrays are a fundamental data structure in Java that stores data of the same type in contiguous memory locations. Removing duplicate elements from an array is a common operation that can be easily accomplished using sets. However, in this article, we will learn how to remove duplicates from an array
2 min read
Java Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu
2 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