**by BehindJava**

# How to remove duplicates and sort the array without using inbuilt functions like .sort or sorting algorithms from an unsorted array

In this tutorial, we are going to learn about implement the Java program to remove the duplicates and sort the array from an unsorted array without using inbuilt functions .sort or sorting algorithms.

To do this, we can traverse the array of elements into the SortedSet and TreeSet.

# java.util.SortedSet

A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set’s iterator will traverse the set in ascending element order. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.)

# java.util.TreeSet.TreeSet()

Constructs a new, empty tree set, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add call will throw a ClassCastException.

```
import java.util.SortedSet;
import java.util.TreeSet;
public class Duplicate {
public static void main(String[] args) {
// Unsorted array with duplicates
Integer[] arr = new Integer[] { 78, 34, 1, 3, 90, 34, -1, -4, 6, 55, 20, -65, 55, 78, 3 };
// Removes duplicates and sort the array
SortedSet<Integer> set = new TreeSet<Integer>();
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]);
}
set.forEach(n -> System.out.print(n + " "));
}
}
```

**Output:**

`-65 -4 -1 1 3 6 20 34 55 78 90 `