Remove Duplicate Elements from an Array using jQuery

A very useful jQuery function is the $.unique() that removes all duplicate elements from an array of DOM elements. However this function only works on arrays of DOM elements, not strings or numbers. Thanks to an anonymous reader for pointing this out in the comments section. Here's an updated code on how to remove duplicate elements from an array of integers.

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title>Remove duplicate elements from array (from DevCurry.com)</title>
<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript">
$(function () {
var arr = [1, 2, 3, 2, 6, 4, 3, 7];
$("#orig").html("Original array :" + arr.join(","));
arr = $.unique(arr);
$("#uniq").html("Unique array :" + arr.sort().join(","));
});
</script>
</
head>

<
script type="text/javascript">
$(function () {
var arr = [1, 2, 3, 2, 6, 4, 3, 7];
$("#orig").html("Original array :" + arr.join(","));
arr = arr.unique();
$("#uniq").html("Unique array :" + arr.sort().join(","));
});

// Original function by Alien51
Array.prototype.unique = function () {
var arrVal = this;
var uniqueArr = [];
for (var i = arrVal.length; i--; ) {
var val = arrVal[i];
if ($.inArray(val, uniqueArr) === -1) {
uniqueArr.unshift(val);
}
}
return uniqueArr;
}

</script>
</
head>
<
body>
<
div>
<
p id="orig"></p>
<
p id="uniq"></p>
</
div>
</body>
</
html>

The Array.prototype extends the definition of the array by adding properties and methods. Here we have created a custom function called unique which removes duplicates from an array.

Note: In case of a string array, the search is case-sensitive. So the function does not consider “Put” and “put” as duplicate

OUTPUT

image

See a Live Demo



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

6 comments:

Anonymous said...

It doesn't work dude... as the API states:

'Remove all duplicate elements from an array of elements. Note that this only works on arrays of DOM elements, not strings or numbers.'

Anonymous said...

Dude...U serious, this only works on DOM elements (sad but true) not TEXT or NUMBERS.

//Limp

Anonymous said...

dude i tried your demo it doesn't work at all

Suprotim Agarwal said...

Sorry guys! The code and the demo have been updated. Please check it again.

Michael Mior said...

Thanks! It seems like the function by Alien51 should really be included in jQuery.

Suprotim Agarwal said...

I second that! It's a nice piece of code. Btw, do you know who's Alien51? A google search doesn't help here.

I want to thank him for this piece of code which I found online.