November 11, 2014 · javascript

WSH Javascript remove duplicates from array

How to remove duplicates from array in Javascript?

First, let's see what the problem is. We have an unsorted array with integer numbers.

 20, 10, 9, 4, 9, 30, 20, 4, 9

We expect our algorithm to output sorted array without any duplicates.

Step 1 - Sort array

The first step is to sort array of numbers. Sorting an array alphabetically can be achieved by calling following function

 array.sort();

But what if you want to sort numbers? When executed without any parameters, javascript built-in sort() function produces the following result:

 10, 20, 20, 30, 4, 4, 9, 9, 9

Array.sort() accepts optional parameter in the form of function pointer. The only thing you need to do is to provide the method that defines sort order(compares two numbers).

 array.sort(function (a,b) { return a - b });

Less than 0: Sort "a" to be a lower index than "b"
Zero: "a" and "b" should be considered equal, and no sorting performed.
Greater than 0: Sort "b" to be a lower index than "a".

You can always implement some custom sort function. In this example insertion sort implementation is provided as alternative to built-in javascript sort function.

Step 2 - Remove duplicates

Loop through array starting with the second element. If the current and the previous elements in the array are the same, delete current element. At the end you will have sorted array without duplicates.

 for (var i = 1; i < array.length;) {
     (array[i - 1] == array[i]) ? array.splice(i, 1) : i++;
 }

Complete source code listing

 //@author Stankovic Vlada [svlada@gmail.com]

 var testArrays = {
     sorted: [1, 2, 2, 3, 4, 5, 5, 5, 6, 6, 7],
     unsorted: [20, 10, 9, 4, 9, 30, 20, 4, 9]
 }

 removeDuplicates(testArrays.sorted);
 printArray(testArrays.sorted);

 removeDuplicates(testArrays.unsorted);
 printArray(testArrays.unsorted);

 function removeDuplicates(array) {
     array.sort(function (a,b) { return a - b });
     // insertionSort(array);
     printArray(array);
     for (var i = 1; i < array.length;) {
         (array[i - 1] == array[i]) ? array.splice(i, 1) : i++;
     }
     return array;
 }

 function printArray(array) {
     WScript.echo(array);
 }

 function insertionSort(list) {
     for (var j = 1; j < list.length; j++) {
         var key = list[j];
         var i = j - 1;
         while (i >= 0 && key < list[i])  {
             list[i+1] = list[i];
             i = i - 1;
         }
         list[i+1] = key;
     }
 }

Follow me on Twitter

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus