JQuery | unique() method

This unique() Method in jQuery is used to sorts an array of DOM elements, in place, with the duplicates removed.

Syntax:

jQuery.unique( array )

Parameters: The unique() method accepts only one parameter that is mentioned above and described below:

  • array : This parameter is the Array of DOM elements.

Return Value: It returns the sorted array of DOM elements after removing the duplicates.

Example 1: In this example, the unique() method removes duplicate elements from the array of divs.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery | unique() method</title
</script>
<style>
    div {
        color: blue;
    }
</style>
</head>
<body style="text-align:center;"
      
    <h1 style="color: green"
        GeeksForGeeks 
    </h1
      
    <h3>JQuery | unique() method</h3>
    <div></div>
    <div class="geek"></div>
    <div class="geek"></div>
    <div class="geek"></div>
    <div></div>
    <script>
    $(function () { 
          
        var divs = $( "div" ).get();
          
        divs = divs.concat( $( ".geek" ).get() );
        $( "div:eq(1)" ).text( 
"Sorts an array of DOM elements " + divs.length +
 " with the duplicates removed" );
      
        divs = jQuery.unique( divs );
        $( "div:eq(2)" ).text( 
"Sorts an array of DOM elements " + divs.length +
 " with the duplicates removed" )
            .css( "color", "red" );
    })
    </script>
  
</body>
</html>                    

chevron_right


Output:

Example 2: In this example, the unique() method removes all duplicate elements from the array of p’s.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery | unique() method</title
  
<script src
    </script
  
</head>
<body style="text-align:center;"
      
    <h1 style="color: green"
        GeeksForGeeks 
    </h1
      
    <h3>JQuery | unique() method</h3>
    <p></p>
    <p class="geek"></p>
    <p></p>
    <script>
    $(function () { 
          
        var ps = $( "p" ).get();
        ps = jQuery.unique( ps );
        $ UniqueSort (document getElementsByTagName ( "p".));
        $( "p" ).text( 
"Sorts an array of DOM elements with the duplicates removed" )
            .css( "color", "red" );
    })
    </script>
  
</body>
</html>                    

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.