jQuery unique() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.unique(array) (alias $.unique) sorts an array of DOM elements in place, removes duplicate node references, and returns that array in document order (added in jQuery 1.1.3). Since jQuery 3.0 it is only an alias of $.uniqueSort().

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.unique(domArray)  → same array, sorted + deduped

// Duplicate = same node object (identity), not same HTML
// DOM elements only — not strings or numbers

// Modern name (same function in jQuery 3+):
$.uniqueSort(domArray)
CallWhat it does
unique()Learn jQuery.unique(): the deprecated DOM dedupe helper (alias of uniqueSort), remove duplicate node references, sort into document order, migrate to $.uniqueSort, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build a plain array of DOM elements (.get() / .toArray()).
Step 2Optionally merge arrays with $.merge or concat.
Step 3Call $.unique(arr) — or $.uniqueSort(arr) in new code.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Duplicate Node References

The same DOM node listed three times collapses to one.

jQuery
var target = document.getElementById("panel");
var nodes = [target, target, target];

console.log(nodes.length);  // 3
$.unique(nodes);
console.log(nodes.length);  // 1
Try It Yourself

How It Works

  1. One element. target is a single DOM node.
  2. Three slots. The array holds three references to that same object.
  3. Unique. $.unique keeps one copy and sorts the array.

Example 2 — Look-Alike Nodes Stay Separate

Different elements with the same class are not duplicates.

jQuery
var items = $("li.item").get();  // three different <li> nodes

console.log(items.length);  // 3
$.unique(items);
console.log(items.length);  // 3  (still three nodes)
Try It Yourself

How It Works

  1. Three elements. Each <li class="item"> is its own DOM object.
  2. Same class ≠ same node. Attributes and text are ignored for dedupe.
  3. Unique. Nothing is removed because no reference repeats.

Example 3 — Sorted Into Document Order

Since jQuery 1.4, results are always returned in document order.

jQuery
var nodes = [
  document.querySelector("#third"),
  document.querySelector("#first"),
  document.querySelector("#second")
];

$.unique(nodes);

console.log(nodes.map(function (el) { return el.id; }).join(" → "));
// "first → second → third"
Try It Yourself

How It Works

  1. Shuffled input. Nodes start as third, first, second.
  2. Sort. $.unique reorders them as they appear in the document.
  3. No dupes here. Length stays 3; only order changes.

Frequently Asked Questions

jQuery.unique(array) sorts an array of DOM elements in place, removes duplicate node references, and returns that array in document order. It only works on DOM elements — not strings, numbers, or plain objects.
Yes. Since jQuery 3.0, $.unique() is deprecated and kept as an alias of $.uniqueSort(). Both names call the same function. Prefer $.uniqueSort() in new code.
Two entries are duplicates only when they are the exact same DOM node object. Two different elements with identical class or text are not duplicates — jQuery compares node identity, not attributes.
No. Pass a native array of DOM nodes. Convert with .get() or .toArray() first: var nodes = $("div").get(); $.unique(nodes);
No. For primitives, use a Set or Array.filter() instead. $.unique is a DOM-only helper, mainly used inside jQuery.
Prefer $.uniqueSort() for new code. Treat $.unique() as the legacy name you may still see in older tutorials — migrating is a rename with no behavior change in jQuery 3+.

Next: Properties

Learn how jQuery returns accurate type strings for values.

Properties tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful