April 8, 2010

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



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

6 Responses to "Remove Duplicate Elements from an Array using jQuery"
  1. Anonymous said...
    August 31, 2010 2:30 AM

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

  2. Anonymous said...
    December 8, 2010 3:30 AM

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

    //Limp

  3. Anonymous said...
    December 13, 2010 6:52 PM

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

  4. Suprotim Agarwal said...
    December 13, 2010 9:12 PM

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

  5. Michael Mior said...
    January 7, 2011 8:28 PM

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

  6. Suprotim Agarwal said...
    January 7, 2011 8:32 PM

    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.

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions