Lodash _.sortedUniq() method
What you’ll learn
- How
_.sortedUniq(array)keeps the first element of every contiguous equal run. - Why it runs in linear time by leaning on sorted ordering.
- When
uniqor sorting first is the safer choice. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Knowing how _.sortedIndex()-style ordering works helps you validate inputs before trusting neighbor comparisons.
- You understand shallow uniqueness—nested arrays or objects still dedupe only when references repeat adjacently.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.sortedUniq is Lodash’s streamlined deduper when duplicates arrive back-to-back—perfect after sorting numeric IDs, timestamps, or enumerated ranks.
Linear scan
One forward pass compares each element only with its predecessor—no hash tables required.
Stable survivors
The earliest occurrence inside each duplicate plateau survives—downstream consumers keep deterministic ordering.
Fresh array
Inputs remain untouched; assign or chain further Lodash calls on the returned copy.
Syntax
_.sortedUniq(array) - array: values presumed ascending sorted so duplicates appear in contiguous blocks.
- Returns: new array omitting repeated neighbors via SameValueZero checks.
Collapse consecutive duplicates
Each plateau collapses to its leading element while overall ascending order stays intact.
import sortedUniq from "lodash/sortedUniq";
sortedUniq([1, 1, 2, 2, 2, 3, 3]);
// → [1, 2, 3] Already unique
Distinct ascending sequences round-trip into a new array reference sharing identical ordering.
import sortedUniq from "lodash/sortedUniq";
const xs = [10, 20, 30];
const out = sortedUniq(xs);
// out → [10, 20, 30]; out !== xs Unsorted danger
Values separated by smaller intermediaries survive independently—sort first when you need global uniqueness.
import sortedUniq from "lodash/sortedUniq";
sortedUniq([2, 1, 1, 2]);
// → [2, 1, 2] — duplicate 2 remains because runs were not contiguous 📋 _.sortedUniq vs _.uniq vs _.sortedUniqBy
| API | Input expectation | Complexity flavor |
|---|---|---|
_.sortedUniq(array) | Ascending sorted neighbors | Linear single pass |
_.uniq(array) | Any order | Tracks seen values globally |
_.sortedUniqBy(array, iteratee) | Sorted by projected key | Linear pass with iteratee projections |
Pitfalls to avoid
Skipping sort
Partial uniqueness masks duplicated values that appear on both sides of unrelated entries.
Structural duplicates
Distinct object literals never compare equal—flatten ids first when semantics demand deduping rows.
Duplicate NaN runs
SameValueZero folds neighboring NaN placeholders exactly like other primitives.
❓ FAQ
Summary
- Purpose:
_.sortedUniq(array)collapses consecutive duplicates on sorted ascending input. - Alternative: resort then call
sortedUniq, or useuniqwhen ordering is unknown. - Next: Lodash _.sortedUniqBy(), _.sortedLastIndexOf() (previous), or the array methods hub.
Because sortedUniq only compares neighbors, it matches best right after a sort—or right before _.sortedIndexOf()-style analytics—when identical values already sit in contiguous blocks.
6 people found this page helpful
