An emitting Observable sequence could emit duplicates by error or by design. The distinct()
and distinctUntilChanged()
functions allow us to deal smoothly with duplicates.
What if we want to be absolutely sure that we process a specific value only once? We can get rid of the duplicates by applying the distinct()
function to our Observable sequence. Like takeLast()
, distinct()
works with a completed sequence and to achieve this duplication filtering, it needs to keep a track of every emitted item. This is a memory usage concern that you have to keep in mind if you are dealing with a huge sequence or big emitted items.
The next figure shows how we can create a sequence with no duplicate working on an Observable
source that emits numbers and emits 1
and 2
twice:
To create our example sequence, we are going to use a few methods we have learned so far: