Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Remove Duplicate Entries from an Array using TreeSet

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:

  1. Create an object of TreeSet.
  2. Add array elements to the TreeSet using the add() method.
  3. Print and display elements in an array that are duplicated using add(element) method.
  4. Print and display elements in an array after removing duplicates from it.
  5. Getting size of TreeSet using size() method.
  6. Converting above TreeSet to arrays using toArray() method for conversion.
  7. Iterating over array elements.
  8. 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));
    }
}


 
 

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]

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments