Question
There is a function listUniq
, but it returns unique values even if the value was not unique:
=: listUniq(list: $list)
list: listCreate(item: "Alex"; item: "Sam"; item: "Alex")
Result: ["Alex","Sam"]
My task is to return a list of unique values, but if a value is not unique, it should be removed.
So, if we take the example from the documentation, according to my task, the code should return only ["Sam"]
, since "Alex"
is a non-unique value in the list.
Answer
To solve this task, it is necessary to process the list using listReplace, adding a column with the count of occurrences of each element in the list.
For this, we pass to listReplace a list of unique values obtained through listUniq and at each step of listReplace, we filter the original list by the value in the current step and record the count of occurrences in the count column.
Then we filter the resulting rowlist by the count column where the values are equal to 1 and take the value column from the filtered rowlist:
=: $filter[[value]]
filter: listFilter(list: $replace; key: "count" = 1)
replace: listReplace(list: $rowlist; action: "count" = $count; value: "value")
rowlist: rowListCreate(field: "value" = $uniq)
uniq: listUniq(list: $list)
~list: listCreate(item: "Alex"; item: "Sam"; item: "Alex"; item: "John")
count: listCount(list: $inner_filter)
inner_filter: listFilter(list: $list; key: "value" = $#value[value])