Finding the unique elements of a list is a common task, often tied to detecting or removing duplicate values. There are two versions and accompanying methods.
- Unique elements by key
- Unique elements by value
The choice depends on the presense of a “unique key.” That is, if you can construct a unique key from a set of fields, then use option 1, else you will need to use option 2. Prefer option 1 as option 2 will have much worse runtimes as the input grows.
Unique Elements by Key
The goal here is to detect and remove duplicates based on a key or set of fields. The way we do this is to turn the list into a “map” using struct comprehension and then convert it back to a list.
Unique Elements by Value
The goal here is to detect and remove duplicates based on a the entire value. We use the list package and list comprehension.