Skip to main content
Browse topics

Finding Duplicates in Array using Treeset Example in Java

2 min read Updated May 29, 2026
Share

Introduction

Finding Duplicates in Array Using TreeSet is a classic Java console program that demonstrates the concept with complete source code and sample output. The Collections Framework provides ArrayList, HashMap, HashSet and related data structures.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

TreeSet uses a red-black tree implemented by a java.util.TreeMap. The red-black tree makes sure that there are no duplicates. Additionally, it allows TreeSet to implement java.util.SortedSet. Thus TreeSet itself sorts the values in it.

Syntax

java
TreeSet<variable-type> variableName = new TreeSet<variable-type>();

Example Program

java
import java.util.Arrays;
import java.util.TreeSet;

public class DuplicatesInArrayUsingTreeSet {

    public static void main(String[] args) {
        String[] array = new String[]{"a", "d", "z", "x", "t", "b", "a", "z"};
        System.out.println("Input Array is : " + (Arrays.toString(array)));
        TreeSet<String> treeSet = new TreeSet<String>();
        for (String str : array) {
            if (!treeSet.add(str)) {
                System.out.println("Duplicate Entry is: " + str);
            }
        }
        System.out.println("TreeSet is : " + treeSet);
    }
}

Sample Output

plaintext
Input Array is : [a, d, z, x, t, b, a, z]
Duplicate Entry is: a
Duplicate Entry is: z
TreeSet is : [a, b, d, t, x, z]

When to use

Use this finding duplicates in array using treeset example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Arrays; imports a class used later in the program.

  3. import java.util.TreeSet; imports a class used later in the program.

  4. String[] array = new String[]{"a", "d", "z", "x", "t", "b", "a", "z"}; updates a variable used in the calculation or output.

  5. A println / print call writes text to the console — part of the sample output below.

  6. TreeSet<String> treeSet = new TreeSet<String>(); updates a variable used in the calculation or output.

  7. The if statement runs the nested code only when the condition is true.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

Common Mistakes

Frequently Asked Questions

What does the Finding Duplicates in Array Using TreeSet program demonstrate?
It shows how to implement finding duplicates in array using treeset in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related tutorials

Search tutorials