# Unique sort (removing duplicates and sorting an array) in JavaScript

As the problem stated, create a program for a unique sort in javascript. Basically we have to remove duplicate elements from an array.

## Understand the problem

In this problem statement we need to eliminate the same or duplicated item from an array. For solving this kind of problem we can use some predefined methods of javascript. In this article you will be able to learn usage of forEach(), spread operator, set() method, filter() method and indexOf() methods.

Lets understand this problem with an example.

Array before sorting and with duplicates

[535, 646, 141, 535, 123, 646, 147, 123]

Array after sorting and without duplicates

[123, 141, 147, 535, 646]


## Algorithm - Using filter() function

The algorithm mentioned below will give a step by step process to solve the given problem to solve this problem.

For example if we had given an array of fruits, so precise algorithm to remove duplicates and sorting it as follows:

Step 1: Define an array with some duplicate data.

Step 2: Now the logic will begin from here, as the problem stated we have to eliminate the duplicate data from the above initialized array. In this step we will declare a function named to eliminateDuplicates.

Step 3: In the body of the function we will use a filter function which is a javascript’s predefined function. This function will filter the data as per the conditions mentioned inside this function.

Step 4: The outcome of this function will be the array of sorted and unique elements.

## Code for the algorithm - Using filter() function

### Example

// array with string values
const fruits = ["Apple", "Banana", "Pineapple", "Apple", "Strawberry",
"Pineapple"];

// function to remove duplicates
function eliminateDuplicates() {
return fruits.filter((item,
index) => fruits.indexOf(item) === index);
}
console.log("After removing duplicates and sorted array");
console.log(eliminateDuplicates(fruits));



### Output

After removing duplicates and sorted array
[ 'Apple', 'Banana', 'Pineapple', 'Strawberry' ]


## Algorithm - Using set() function

The algorithm mentioned below will give a step by step process to solve the given problem using the set() function of javascript.

For example if we had given an array of colors, so precise algorithm to remove same elements and sorting it as follows:

Step 1: Define an array with some duplicate data.

Step 2: Now the logic will begin from here. In this step we will declare a function named to eliminateDuplicates and pass colors array in it to initialize with.

Step 3: In the body of the function we will use a Set() function which is a javascript’s predefined function. And in the parameter we will pass an array. This function will set the new data in sorted form and also removes duplicates.

Step 4: The outcome of this function will be the array of sorted and unique elements.

## Code for the algorithm - Using set() function

### Example

// array with string values
const colors = ["Red", "Black", "Pink", "Red", "Silver", "Pink"];

function eliminateDuplicates(colors) {
return [...new Set(colors)];
}
console.log("After eliminating duplicates")
console.log(eliminateDuplicates(colors));



### Output

After eliminating duplicates
[ 'Red', 'Black', 'Pink', 'Silver' ]


## Algorithm - Using forEach() method

Step 1: Define an array with some duplicate data. In our case we have declared an integer data with duplicate values in it.

Step 2: Now, In this step we will declare a function named to eliminateDuplicates and pass numbers array in it to initialize with.

Step 3: In the body of the function we will use a forEach() method. This method will check every element of the array and using a callback function we will check if the elements are the same so push that element in the new array.

## Code for the algorithm - Using forEach() function

### Example

// array with string values
function eliminateDuplicates(numbers) {
const sortedArray = [];

numbers.forEach(function(element, index) {
if (numbers.indexOf(element) === index) {
sortedArray.push(element)
}
});

return sortedArray;
}

console.log("After eliminating and sorting the array")
console.log(eliminateDuplicates([10, 20, 30, 40, 50, 10, 20]));


### Output

After eliminating and sorting the array
[ 10, 20, 30, 40, 50 ]


## Time complexity

Time taken by the above algorithms is O(n). Because all the programs are working on the length of the array, the time complexity to complete all the programs is O(n). All the methods are using javascript’s functions. These functions are only taking array elements to sort and remove the duplicates.

## Conclusion

This is the basic idea behind solving these kinds of problems. In the overall process we have used some predefined functions of javascript, arithmetic operators and comparison operators to solve the problem. And saw how to calculate the time complexity of the algorithms.

Updated on: 18-Aug-2023

3K+ Views

##### Kickstart Your Career

Get certified by completing the course