jQuery unique() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is unique()?

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().

Remember
$.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)
NeedUse
Dedupe DOM nodes (new code)$.uniqueSort(arr)
Dedupe DOM nodes (legacy)$.unique(arr)
Merge node arrays first$.merge(a, b)
Dedupe strings/numbersnew Set(...) (not $.unique)

How to Use unique()

Collect DOM nodes into a plain array, then unique (or uniqueSort) them.

  1. Build a plain array of DOM elements (.get() / .toArray()).
  2. Optionally merge arrays with $.merge or concat.
  3. Call $.unique(arr) — or $.uniqueSort(arr) in new code.
  4. Use the same array afterward (it is mutated in place).
PieceRequired?Role
arrayYesPlain JS array of DOM elements
Return—Same array, sorted + deduped (document order)
Mutation—In place — original array is changed

Pseudocode

Pseudocode
function unique(array):
    // DOM elements only
    sort array into document order
    remove entries that are the same node object
    return array   // mutated in place

// Typical flow:
nodes = $("div").get()
$.merge(nodes, $(".dup").get())
$.unique(nodes)          // or $.uniqueSort(nodes)

Cheat Sheet

Keep these facts handy while reading legacy jQuery code.

TopicDetail
SyntaxjQuery.unique(array) / $.unique(array)
SincejQuery 1.1.3
StatusDeprecated 3.0 — alias of uniqueSort
InputArray of DOM elements only
Duplicate ruleSame node object (===)
OrderDocument order (since jQuery 1.4)
Modern name$.uniqueSort()

Live Preview

Pick a sample node list, run $.unique, and inspect length / order.

Browser demo of $.unique() — same ideas as the examples below.

Result
sample = Same node 3×
before → 3
after  → 1

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. In place. nodes itself shrinks — no new array is created.

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.
  4. Gotcha. Do not expect unique to merge “similar looking” markup.

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.
  4. Why. Document order makes later DOM walks predictable.

Example 4 — Migrate to $.uniqueSort()

In jQuery 3+, both names run the same function.

jQuery
var a = $("nav a").get();
$.merge(a, $(".footer-link").get());
$.unique(a);

var b = $("nav a").get();
$.merge(b, $(".footer-link").get());
$.uniqueSort(b);

console.log(a.length === b.length);  // true
// Prefer $.uniqueSort in new code
Try It Yourself

How It Works

  1. Same merge. Both arrays start from the same selector overlap.
  2. Legacy. $.unique(a) still works on jQuery 3.x.
  3. Modern. $.uniqueSort(b) is the supported name.
  4. Migrate. Rename $.unique → $.uniqueSort with no other change.

Edge Cases & Pitfalls

Check these before calling unique on real node lists.

deprecated

Prefer uniqueSort

Since jQuery 3.0, $.unique is only an alias. Use $.uniqueSort() in new code.

DOM only

Not for strings or numbers

Pass DOM elements only. For primitives, use Set or filter — not $.unique.

identity

Same markup ≠ duplicate

Two different nodes with identical HTML are kept. Only repeated references to one node are removed.

array

Needs a plain array

Do not pass a jQuery object. Convert with .get() or .toArray() first.

Browser Support

jQuery.unique() has been available since jQuery 1.1.3 and is deprecated since jQuery 3.0 as an alias of $.uniqueSort(). Prefer $.uniqueSort() in all modern browsers with jQuery loaded.

✓ Deprecated · jQuery 3.0

jQuery.unique()

Works in Chrome, Firefox, Safari, and Edge with jQuery 1.1.3+. Prefer $.uniqueSort() for new code. DOM-only — not for Node.js without a DOM.

Legacy Prefer uniqueSort
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.unique() Legacy alias

Bottom line: Learn $.unique() for legacy jQuery. Write $.uniqueSort(arr) in new projects.

Performance Notes

ApproachCostBest for
$.uniqueSort(arr) / $.unique(arr)Sort + scanSmall/medium DOM node arrays
Let jQuery merge selectorsOften enoughNormal app code (rarely call unique yourself)
Huge node listsNoticeableAvoid re-running in hot loops

You rarely need this in application code — jQuery uses it internally. When you do, run it once after merging node arrays, not on every click.

Key Takeaways

  • Role: dedupe + sort DOM node arrays in place.
  • Modern: use $.uniqueSort() instead of $.unique().
  • Identity: same node object only — not similar HTML.
  • Status: deprecated in jQuery 3.0.

One line: treat $.unique as the old name for $.uniqueSort — DOM identity dedupe in document order, not a general array unique.

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: type()

Learn how jQuery returns accurate type strings for values.

type() tutorial →

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