- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unique sort (removing duplicates and sorting an array) in JavaScript
The simultaneous technique of removing duplicates and sorting an array is often termed as a unique sort technique.
For example, if the input array is −
const arr = [1, 1, 1, 3, 2, 2, 8, 3, 4];
Then the output should be −
const output = [1, 2, 3, 4, 8];
Example
The code for this will be −
const arr = [1, 1, 1, 3, 2, 2, 8, 3, 4]; const uniqSort = (arr = []) => { const map = {}; const res = []; for (let i = 0; i < arr.length; i++) { if (!map[arr[i]]) { map[arr[i]] = true; res.push(arr[i]); }; }; return res.sort((a, b) => a − b); }; console.log(uniqSort(arr));
Output
And the output in the console will be −
[ 1, 2, 3, 4, 8 ]
- Related Articles
- Removing consecutive duplicates from strings in an array using JavaScript
- Sorting an array of literals using quick sort in JavaScript
- Sorting Array without using sort() in JavaScript
- Count unique elements in array without sorting JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Removing duplicates and keep one instance in JavaScript
- Removing duplicates and inserting empty strings in JavaScript
- How do I make an array with unique elements (remove duplicates) - JavaScript?
- Remove duplicates and map an array in JavaScript
- Sorting array of exactly three unique repeating elements in JavaScript
- Removing adjacent duplicates from a string in JavaScript
- Removing an element from an Array in Javascript
- Alternative sorting of an array in JavaScript
- Sorting an array by date in JavaScript
- Sorting an array by price in JavaScript

Advertisements