Java Program to Remove Duplicate Entries from an Array using TreeSet
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
import java.util.Arrays;
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
String[] input
= new String[] { "Hello" , "hi" , "Wow" ,
"cute" , "thanks" , "hi" ,
"Aww" , "cute" , "baby" ,
"beloved" , "Aww" };
System.out.print(
"Initial String Array(Containing Duplicates) : "
+ (Arrays.toString(input)));
TreeSet<String> dupliCheckr = new TreeSet<String>();
for (String element : input) {
if (!dupliCheckr.add(element)) {
System.out.println(
"Duplicate Data entered : " + element);
}
}
System.out.println();
System.out.println(
"TreeSet(After Removing Duplicates) : "
+ dupliCheckr);
System.out.println();
int length = dupliCheckr.size();
input = dupliCheckr.toArray(input);
for ( int i = length; i < input.length; i++)
input[i] = null ;
System.out.println( "Final String Array is : "
+ Arrays.toString(input));
}
}
|
Output
Initial 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]
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...