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






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 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.

Anonymous 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.