jQuery jQuery.unique() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Legacy DOM dedupe

What You’ll Learn

The jQuery.unique() utility removes duplicate DOM node references from an array and sorts the remaining elements into document order. Deprecated since jQuery 3.0 (alias of uniqueSort), it remains essential for reading legacy tutorials. This page covers syntax, identity-based deduplication, five worked examples, migration to $.uniqueSort(), and when each name appears in the wild.

01

Syntax

$.unique(arr)

02

DOM only

Elements, not strings

03

Identity

Same node ref

04

Doc order

Sorted in page

05

In place

Mutates array

06

Deprecated

Use uniqueSort

Introduction

When you combine DOM results from multiple jQuery selectors, the same element can appear more than once in your working array. jQuery provides jQuery.unique() (also written $.unique()) to clean that list: it strips duplicate node references and orders what remains according to where each element sits in the document.

This name dates back to early jQuery releases. In jQuery 3.0 it was renamed to jQuery.uniqueSort() to avoid confusion with other “unique” APIs, but $.unique() still works as a deprecated alias. You will see it in older blog posts, Stack Overflow answers, and plugins written for jQuery 1.x and 2.x.

Understanding the jQuery.unique() Method

jQuery.unique(array) accepts a native array (or array-like object) whose items are DOM elements. It modifies that array in place, removes entries that point to the same node object twice, and sorts survivors into document order. The return value is the same array reference, now shorter and ordered.

Duplicate detection is strict: only the exact same node counts as a duplicate. Two separate <div> elements with matching classes or ids remain distinct entries. This method does not deduplicate strings, numbers, or plain objects.

💡
Beginner Tip

For new projects on jQuery 3+, prefer $.uniqueSort(). When you see $.unique() in legacy code or tutorials, treat it as the same function — a straight rename is all you need to modernize.

📝 Syntax

General form of jQuery.unique:

jQuery
jQuery.unique( array )
// or
$.unique( array )

Parameters

  • array — a native array or array-like object containing DOM elements. This collection is modified in place.

Return value

  • Returns the same array reference, now deduplicated and sorted in document order.

Minimal workflow

jQuery
var nodes = $("div").get();
nodes = nodes.concat($(".highlight").get());
nodes = $.unique(nodes);

⚡ Quick Reference

GoalCode
Deduplicate DOM nodes (legacy name)$.unique(nodes)
Modern equivalent (jQuery 3+)$.uniqueSort(nodes)
Convert jQuery collectionvar nodes = $(sel).get()
Merge then dedupe$.merge(a, b); $.unique(a)
Works on strings/numbers?No — DOM elements only
Mutates array?Yes — in place

📋 $.unique vs $.uniqueSort vs $.merge

Three utilities that often appear together in legacy jQuery code.

unique
legacy name

Deprecated since 3.0; same behavior as uniqueSort

uniqueSort
current name

Preferred in new code; identical implementation

merge
append all

Concatenate arrays; keeps every entry until unique runs

Pipeline
merge+unique

Classic pattern in jQuery 1.x/2.x tutorials

Examples Gallery

Each example uses DOM elements and $.unique() — the legacy name you will encounter in older jQuery documentation. Open DevTools or use the Try-it links to run them interactively. Example 1 follows the official jQuery API demo pattern.

📚 Getting Started

Remove duplicate DOM references after combining node arrays with the legacy API.

Example 1 — Legacy $.unique() After Merging Div Arrays

Collect all div elements, append duplicates from .dup, then run $.unique() to shrink the list — the pattern shown in the official jQuery API docs.

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

// Add elements matched by .dup (they are divs too)
divs = divs.concat($(".dup").get());

console.log("Pre-unique:", divs.length);

divs = $.unique(divs);

console.log("Post-unique:", divs.length);
Try It Yourself

How It Works

Some .dup nodes were already included in $("div"), so concatenating them adds the same node references again. $.unique() collapses those duplicates and orders the survivors as they appear in the document tree.

📈 Practical Patterns

Identity rules, look-alike elements, document order, and migration to uniqueSort.

Example 2 — Same Node Reference Three Times → One Entry

Push the same element three times manually — only one copy survives after unique.

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

console.log("Before:", nodes.length);

$.unique(nodes);

console.log("After:", nodes.length);
console.log(nodes[0] === target); // true
Try It Yourself

How It Works

All three slots reference the identical DOM node object. unique keeps one entry because duplicates are detected by reference equality (===), not by comparing tag names or attributes.

Example 3 — Look-Alike <li> Elements Stay Separate

Two different <li> elements with the same text remain separate entries.

jQuery
var items = $("li.item").get();

console.log("Before:", items.length);

$.unique(items);

console.log("After:", items.length);
// Still 3 — three distinct node objects in the DOM
Try It Yourself

How It Works

Even when elements look identical in HTML, each is a separate object in memory. unique does not compare innerHTML, classes, or ids — only whether two array slots point to the same node instance.

Example 4 — Document Order Reorder

Build an array in reverse DOM order, then let unique reorder it to match the page.

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

Since jQuery 1.4, unique always returns elements sorted by their position in the document. That predictable ordering matters when jQuery chains selector results before applying DOM operations — one reason the method was later renamed to uniqueSort.

Example 5 — Migration: $.unique(arr)$.uniqueSort(arr)

Upgrading to jQuery 3+? Replace the deprecated name with a drop-in rename — behavior is identical.

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

console.log("Merged length:", nodes.length);

// Before (jQuery 1.x / 2.x style)
// $.unique(nodes);

// After (jQuery 3+ recommended)
$.uniqueSort(nodes);

console.log("Unique length:", nodes.length);

$(nodes).addClass("tracked");
Try It Yourself

How It Works

jQuery 3.0 kept $.unique() as a deprecated alias pointing at the same function as $.uniqueSort(). A global search-and-replace of $.unique( to $.uniqueSort( is safe — no argument or return-value changes are required.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize $.unique() in jQuery 1.x/2.x docs and older blog posts.
  • Maintaining old plugins — understand dedupe logic before renaming to uniqueSort.
  • Combined selectors — dedupe after merging results from multiple jQuery queries.
  • Internal jQuery pipelines — jQuery called this internally before the 3.0 rename.
  • Document-order guarantees — rely on sorted output before iterating for layout-sensitive work.
  • Debugging overlap — inspect array length before/after when selectors unexpectedly double-count nodes.

🧠 How jQuery.unique() Processes an Array

1

Validate input

Expect a native array or array-like object whose items are DOM elements.

Input
2

Sort by document position

Order elements according to their place in the DOM tree (since jQuery 1.4).

Sort
3

Remove duplicate refs

Drop later occurrences when two slots reference the same node object (===).

Dedupe
4

Return same array

The modified array reference is returned with a shorter length.

📝 Notes

  • jQuery.unique() has existed since early jQuery; deprecated in jQuery 3.0 in favor of jQuery.uniqueSort().
  • Since jQuery 1.4, results are always sorted in document order.
  • Only works on arrays of DOM elements — not strings, numbers, or generic objects.
  • Modifies the input array in place; clone first if you need the original list preserved.
  • Convert jQuery collections with .get() or .toArray() before calling.
  • Chiefly an internal jQuery helper — most application code never calls it directly.

Browser Support

jQuery.unique() ships with jQuery since early releases and remains available as a deprecated alias in jQuery 3.x. It works wherever jQuery runs — behavior is consistent because jQuery owns the implementation.

jQuery 1.x+

jQuery jQuery.unique()

Available in all jQuery versions. Deprecated since 3.0 — use uniqueSort() for new code. Requires jQuery loaded; not a standalone browser API.

100% With jQuery loaded
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
jQuery.unique() Universal

Bottom line: Safe in any project that already includes jQuery. In jQuery 3+, prefer $.uniqueSort(). For deduplicating primitive values without jQuery, use Array.from(new Set(arr)) or .filter() with an index check.

Conclusion

The jQuery.unique() utility cleans arrays of DOM elements by removing duplicate node references and sorting survivors into document order. Deprecated since jQuery 3.0, it is now an alias of uniqueSort with identical behavior. Remember that deduplication is identity-based, the input array is mutated, and the method is specialized for DOM nodes — not general-purpose data cleaning.

Use $.uniqueSort() in new code, but keep $.unique() in your toolkit when reading legacy tutorials or upgrading older plugins — a simple rename is all the migration requires.

💡 Best Practices

✅ Do

  • Prefer $.uniqueSort() in new jQuery 3+ code
  • Call .get() on jQuery collections before deduplicating
  • Use after $.merge when selectors overlap
  • Clone the array first if you need the pre-dedupe list
  • Recognize $.unique() when maintaining legacy plugins

❌ Don’t

  • Write new code with $.unique() when uniqueSort is available
  • Pass strings, numbers, or plain objects expecting deduplication
  • Expect look-alike elements with different node refs to merge
  • Pass a jQuery object directly — unwrap to a native array
  • Assume $.unique() and $.uniqueSort() differ in behavior

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.unique()

The legacy name for DOM node deduplication in jQuery.

5
Core concepts
🔢 02

Node identity

Same ref only

Rule
🗃 03

Doc order

Sorted output

Since 1.4
04

Deprecated

Alias since 3.0

History
🔄 05

uniqueSort

Use in new code

Migration

❓ Frequently Asked Questions

jQuery.unique(array) sorts an array or array-like collection of DOM elements in place, removes duplicate node references, and returns the same array sorted into document order. It only works on DOM elements — not strings, numbers, or plain objects.
Yes. Since jQuery 3.0, jQuery.unique() is deprecated and kept as an alias of jQuery.uniqueSort(). Both names call the same function with identical behavior. Use $.uniqueSort() in new code; keep $.unique() in mind when reading older tutorials or maintaining legacy plugins.
Two entries are duplicates only when they reference the exact same DOM node object. Two different div elements with identical ids, classes, or text are not considered duplicates — jQuery compares node identity (===), not attributes or content.
No. Pass a native JavaScript array of DOM nodes. Convert a jQuery collection with .get() or .toArray() first: var nodes = $("div").get(); nodes = $.unique(nodes);
No. The method is designed for DOM element arrays and is chiefly used internally by jQuery. For primitive values, use Set, Array.filter(), or other native deduplication techniques.
Prefer $.uniqueSort() for new code — it is the current name since jQuery 3.0 and avoids confusion with other "unique" APIs. Use $.unique() when you encounter it in legacy codebases, Stack Overflow answers, or jQuery 1.x/2.x documentation; migrating is a simple rename with no behavior change.
Did you know?

Before jQuery 3.0 this method was the only name — jQuery.unique(). The rename to uniqueSort() clarifies that results are sorted into document order, not merely filtered. jQuery 3 still exposes $.unique() as a deprecated alias so old scripts keep working without modification.

Continue to jQuery.type()

Learn how jQuery returns accurate type strings for null, arrays, dates, and more.

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.

6 people found this page helpful