![]() |
VOOZH | about |
Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows:
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:
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
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]