Full answer
distinctUntilChanged() remembers the last value that it let through. When a new value arrives, it compares the two. If they are equal, the new value is dropped. If not, it is emitted and becomes the new "last value".
Important: it compares only with the previous value, not with every earlier one. The sequence 1, 1, 2, 1 becomes 1, 2, 1. For a full unique list, use a Set, or the distinct() operator (which remembers everything and can leak memory on long streams).
Search box use: a user types "ang", pauses, types "x", and deletes it again. After debounceTime(300) you get "ang", "angx", "ang". Without more filtering, the app would send a request for "ang" twice in a row, and (with switchMap) cancel and restart work for the same value. A pipe that is usually written is:
valueChanges.pipe(debounceTime(300), map(t => t.trim()), distinctUntilChanged(), switchMap(term => api.search(term)))
The order is deliberate: debounce first to reduce the events, normalise (trim, lower case) so that "Ang " and "ang" count as equal, then drop duplicates, then call the server.
Comparing objects. By default the check is ===, so two different objects with the same content are "different". Two options:
- A comparator:
distinctUntilChanged((a, b) => a.id === b.id). Returning true means "same". - A key selector (the second argument):
distinctUntilChanged((a, b) => a === b, user => user.id), which compares the selected keys; ordistinctUntilKeyChanged('id').
Also remember NaN: the default comparison is ===, and NaN !== NaN, so repeated NaN values pass through.
With signals, computed already skips equal values (it uses Object.is), so distinctUntilChanged is mostly needed on observable pipes such as form valueChanges, route params and WebSocket streams.
import { distinctUntilChanged, from, map, of } from 'rxjs';
of(1, 1, 2, 1).pipe(distinctUntilChanged()).subscribe((v) => console.log('number', v));
from(['ang', 'Ang ', 'ANG', 'angular', 'angular ']).pipe(
map((t) => t.trim().toLowerCase()),
distinctUntilChanged(),
).subscribe((t) => console.log('search', t));
interface User { id: number; name: string }
const users: User[] = [{ id: 1, name: 'Asha' }, { id: 1, name: 'Asha' }, { id: 2, name: 'Ravi' }];
from(users).pipe(distinctUntilChanged()).subscribe((u) => console.log('default', u.id));
from(users).pipe(distinctUntilChanged((a, b) => a.id === b.id)).subscribe((u) => console.log('comparator', u.id));
from(users).pipe(distinctUntilChanged((a, b) => a === b, (u) => u.id)).subscribe((u) => console.log('key', u.id));
of(NaN, NaN).pipe(distinctUntilChanged()).subscribe((v) => console.log('nan', v));RxJS 7.8. The script was run in Node 24 twice with identical output. It prints the plain numbers case, the typed text case, and the user objects with and without a comparator. The NaN statement was checked in that run.
Key points
- It drops a value equal to the previous one, comparing only with the last passed value.
- In search: debounce, normalise, `distinctUntilChanged`, then `switchMap`.
- For objects use a comparator or a key selector, because the default is `===`.
- `computed` signals already skip equal values.
Common mistakes
- Using it before `debounceTime` and getting no benefit.
- Expecting `1, 1, 2, 1` to become `1, 2`.
- Comparing objects without a comparator.
Follow-ups
- How is it different from `distinct()`?
- How would you compare users by `id`?
- Why do you normalise the text first?
Tips
- Give the `1, 1, 2, 1` example.
- State the order of operators in a search box.
- Mention that `computed` handles equality for signals.
Evaluation rubric
- Explains previous-value comparison.
- Places it correctly in a search pipe.
- Uses comparator or key selector for objects.