ARTICLE

Use jQuery Unique function to removes duplicate array elements

Posted by Sapna Articles | ASP.NET using VB.NET March 28, 2011
Here we will discuss how to use the jQuery Unique function to removes duplicate elements only from an array of DOM elements.
 
Reader Level:

The jQuery $.unique() function is used to removes duplicate elements only from an array of DOM elements. In the following article we have created a custom function called unique which removes duplicates from an array.

Example

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<
title>jQuery Example</title>
    <script type="text/javascript"
    src="jquery.js">
    </script>
    <script type="text/javascript">
        $(function () {
            var arr = [10, 28, 33, 28, 64, 48, 33, 79, 48, 95];
            $("#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 = [10, 28, 33, 28, 64, 48, 33, 79, 48, 95];
        $("#orig").html("Given array are as follows :" + arr.join(","));
        arr = arr.unique();
        $("#uniq").html("Now the unique array are :" + 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 style="font-family: Verdana; font-size: small">
    <div>    
        <p id="orig"></p
>
        <p id="uniq"></p>
    </div>
</body>
</
html>

Output 

array.gif

Note: The search is case-sensitive in case of a string array as the function does not consider "Out" and "out" as duplicate.

 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor