jQuery unique() example

jQuery unique() function is used to sort an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.


jQuery.unique( array )
array - The Array of DOM elements.


Removes any duplicate elements from the array of divs.
<!doctype html>
<html lang="en">

    <meta charset="utf-8">
    <title>jQuery.unique demo</title>
        div {
            color: blue;
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>


    <div>There are 6 divs in this document.</div>
    <div class="dup"></div>
    <div class="dup"></div>
    <div class="dup"></div>

        // unique() must take a native array
        var divs = $("div").get();

        // Add 3 elements of class dup too (they are divs)
        divs = divs.concat($(".dup").get());
        $("div").eq(1).text("Pre-unique there are " + divs.length + " elements.");

        divs = jQuery.unique(divs);
        $("div").eq(2).text("Post-unique there are " + divs.length + " elements.")
            .css("color", "red");


There are 6 divs in this document.
Pre-unique there are 9 elements.
Post-unique there are 6 elements.
Try it Yourself - Copy paste code in Online HTML Editor to see the result

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course