How to find unique items in a javascript array

In this quick tutorial I’m going to show you how you can find the unique items in a javascript array. To demonstrate what do I mean when I say unique here’s an example:

I have an array called items which contains a set of letters, some are repeated and some are not.

var items = ['a', 'b', 'c', 'd', 'd', 'e', 'a', 'f', 'g', 'a'];

And when I try to get the unique items I should get this:

["b", "c", "e", "f", "g"];

The fact that you’re still reading this means that this is the one that you’re looking for. There’s a lot of ways to do this but here’s the first solution that I came up with:

First, declare an array which will hold the unique items:

var nopairs = [];

Then a variable which will temporarily hold the item which has a pair. This is the current non-unique item in the array.

var haspair = ' ';

And then declare the variable which will hold the current count of the unique items. Initialize its value to zero.

var nopair_ctr = 0;

Next, sort the array using the .sort() method in javascript and then assign it to a variable called arranged.

var arranged = items.sort();

Next, use the jquery.each() method to loop through the sorted array:

$(arranged).each(function(index){
  
});

Inside the loop we checked if the current item is not equal to the next item. Since the array has already been sorted we are assured that the items with the same value are always beside each other. This means that if we do it like this, it will always return false if the current item is equal to the next item.
We also checked if current item is not equal to the current item in the haspair variable. The haspair variable contains the last non-unique item in the array.
If the condition is met, we then store the current item inside the nopairs array using the nopair_ctr as its index. Finally we increment the nopair_ctr by 1 so that we won’t be storing the unique items on the same index.

if(arranged[index] != arranged[index + 1] && arranged[index] != haspair){
	nopairs[nopair_ctr] = arranged[index];
	nopair_ctr++;
}

If the condition above is not met, then we just change the value of the haspair variable to the current item in the array:

else{
	haspair  = arranged[index];			
}	

Finally if you try to output the contents of the nopairs array, you will only see the unique items in the array.

console.log(nopairs);

Life could have been a lot more easier if jquery’s unique() method does it:

console.log(jQuery.unique(items.sort()).sort());

But no, it doesn’t do it since it just pops out the duplicated items in an array and comes up with the following output.

["a", "b", "c", "d", "e", "f", "g"]

 

Conclusion

That’s it for the first article for this new year. You have learned how to use jquery and javascript to sort an array using the .sort() method in javascript, you also learned how to use jQuery’s unique() method to remove duplicates in array, lastly you have learned how to find the unique items in a javascript array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s