Example 1 — Official Sample
Collapse runs of equal numbers — the sample from the Ramda docs — and see that spread-out repeats stay.
console.log(R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2])); // [1, 2, 3, 4, 2]
console.log(R.dropRepeats([1, 2, 1])); // [1, 2, 1] How It Works
- Runs collapse.
1, 1, 1becomes1,4, 4becomes4, and2, 2becomes2. - Neighbours only. The second
2follows a4, so it is a new run and stays. - No neighbours. In
[1, 2, 1]no two equal items touch, so nothing is removed. - New array. Both calls return fresh arrays; the inputs are unchanged.
