Ramda.js R.uniqWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.uniqWith(pred, list) removes duplicates using your own test. pred looks at two items and returns true when they count as the same; the first item of each match is kept (official Ramda docs).

Stable since v0.2.0: R.uniqWith is not deprecated, experimental, or non-standard. Note: your test must return true for duplicates — it is not a sort comparator.

Syntax

Call R.uniqWith. Start with this line.

Ramda
R.uniqWith(pred, list) or R.uniqWith(pred)(list)
CallWhat it does
R.uniqWith(pred, list) or R.uniqWith(pred)(list)Primary signature.
R.uniqWith(...)Call the list helper.

Prefer import { uniqWith } from "ramda" when you want a named import.

Library declarations
Ramda
R.uniqWith(pred, list) or R.uniqWith(pred)(list)

Parameters

ParameterDescription
predRequired. Called as pred(newItem, keptItem); return true for a duplicate
listOptional. The array to clean up; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array; the input is not changed

Ramda Examples

Example 1 — Official: Same Text Counts as Duplicate

Treat 1 and '1' as the same value and keep whichever comes first.

Ramda
const strEq = R.eqBy(String);

console.log(R.uniqWith(strEq)([1, '1', 2, 1]));  // [1, 2]
console.log(R.uniqWith(strEq)(['1', 1, 1]));     // ['1']
Try It Yourself

How It Works

  1. The test. R.eqBy(String) builds (a, b) => String(a) === String(b).
  2. First list. 1 is kept; '1' and the last 1 both match it and are dropped; 2 is new.
  3. Second list. '1' comes first now, so the string is kept and both numbers are dropped.
  4. Lesson. The test decides what matches; list order decides which item survives.

Example 2 — Drop Numbers That Are Too Close

Keep only readings that are at least 0.5 apart from every reading already kept.

Ramda
const close = (a, b) => Math.abs(a - b) < 0.5;

console.log(R.uniqWith(close, [1, 1.2, 2, 2.4, 5]));  // [1, 2, 5]
Try It Yourself

How It Works

  1. Two-item rule. close uses Math.abs to measure the gap between two numbers — no single key can do that.
  2. 1.2 dropped. It is only 0.2 away from the kept 1.
  3. 2 kept, 2.4 dropped. 2 is 1 away from 1; 2.4 is only 0.4 away from 2.
  4. 5 kept. It is far from every kept number, so the result is [1, 2, 5].

Example 3 — Match on Two Fields

Treat people with the same name and city as one person, whatever their other fields say.

Ramda
const people = [
  { name: 'Ana', city: 'Pune', age: 30 },
  { name: 'Ben', city: 'Pune', age: 25 },
  { name: 'Ana', city: 'Pune', age: 31 },
  { name: 'Ana', city: 'Goa', age: 22 }
];

const samePerson = (a, b) => a.name === b.name && a.city === b.city;

console.log(R.uniqWith(samePerson, people));
// Ana (Pune, 30), Ben (Pune, 25), Ana (Goa, 22)
Try It Yourself

How It Works

  1. The test. samePerson returns true only when both name and city match.
  2. Duplicate found. The third person (Ana, Pune, 31) matches the first, so it is dropped — the first version (age 30) stays.
  3. Different city. Ana from Goa fails the city check against every kept person, so she stays.
  4. One field only? For a single field, R.eqProps('name') builds the same kind of test for you.

Example 4 — The Same Pair in Either Order

A match between A and B is the same as a match between B and A.

Ramda
const samePair = (a, b) =>
  (a[0] === b[0] && a[1] === b[1]) || (a[0] === b[1] && a[1] === b[0]);

const games = [['A', 'B'], ['B', 'A'], ['A', 'C'], ['C', 'A'], ['B', 'C']];

console.log(R.uniqWith(samePair, games));
// [['A', 'B'], ['A', 'C'], ['B', 'C']]
Try It Yourself

How It Works

  1. Two checks. samePair is true if the pairs match as written or when one is flipped.
  2. Flipped copies dropped. ['B', 'A'] matches the kept ['A', 'B']; ['C', 'A'] matches ['A', 'C'].
  3. New pair kept. ['B', 'C'] matches nothing kept, so it stays.
  4. Why not uniq? R.uniq sees ['A', 'B'] and ['B', 'A'] as different arrays and keeps all five.

Example 5 — Clean Up a Tag List

Trim the spaces, then drop tags that differ only by letter case.

Ramda
const cleanTags = R.pipe(
  R.map(R.trim),
  R.uniqWith(R.eqBy(R.toLower))
);

console.log(cleanTags(['JS', ' js', 'Ramda', 'RAMDA ', 'css']));
// ['JS', 'Ramda', 'css']
Try It Yourself

How It Works

  1. Trim. R.map with R.trim turns ' js' into 'js' and 'RAMDA ' into 'RAMDA'.
  2. Compare. R.eqBy(R.toLower) says two tags match when their lowercase forms are equal.
  3. First spelling wins. 'JS' and 'Ramda' are kept; 'js' and 'RAMDA' are dropped.
  4. Pipe. R.pipe runs both steps left to right as one reusable function.

Browser & Runtime Support

R.uniqWith() is plain ES5 JavaScript inside Ramda 0.2.0+. It only loops and calls your test, so it needs no Set, Map, or other modern built-ins.

✓ Ramda 0.2.0+ · Any JS runtime

Ramda.js R.uniqWith()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun. The examples on this page add const and arrow functions (ES2015), which every modern browser supports.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.uniqWith() Universal

Bottom line: Safe in any project with Ramda 0.2.0 or later (check with npm ls ramda). These labs load [email protected], where R.uniqWith also works as a transducer.

Frequently Asked Questions

R.uniqWith(pred, list) removes duplicates using your own test. pred receives two items and returns true when they count as duplicates. The first item of each matching group is kept, in the original order. The official sample R.uniqWith(R.eqBy(String))([1, '1', 2, 1]) returns [1, 2].
As pred(newItem, keptItem). Each new item is compared with the items already kept, from first to last, and dropped as soon as one comparison returns true. For symmetric tests like equality the order does not matter.
Use R.uniqBy when you can compute a single key per item, such as an id; it is much faster. Use R.uniqWith when the rule needs both items at once, such as 'numbers within 0.5 of each other' or 'the same pair in either order'.
No. R.uniqWith treats any truthy return value as 'duplicate', so a comparator drops items that differ and keeps items that are equal (0 is falsy). Always return true or false, for example (a, b) => a === b.
No. It returns a new array and leaves the input untouched. An empty list returns []. Passing null or undefined as the list throws a TypeError.
No. R.uniqWith has been a stable Ramda list function since v0.2.0. It is not deprecated, experimental, or non-standard.

Next: R.unnest()

Flatten a list of lists by exactly one level.

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