My beginning is a file with my own Param in the metadata: features: [dynamic_content, segmentation, ai, ai, segmentation]
This can be much longer and include many repetitions of each item.
What I want to achieve is that Hugo lets me know when there are duplicates and what they are exactly, as part of a bigger partial that parses this metadata and creates links to other files.
{{ $relations := .Page.Param "features" }}
{{ $unique := $relations | uniq }}
<!-- Locate duplicate values in param array -->
{{ if not (eq $relations $unique)}}
{{ $duplicates := (collections.SymDiff $unique $relations) }}
{{ printf "Duplicate feature(s) %s found in %s" $duplicates .Page.File }}
{{ end }}
The output is Duplicate feature(s) [] found in article-test
symdiff: returns the symmetric difference of two collections.
{{ $a | symdiff $b }} : [zero one one three five four six]
These two below will return [] because again, "dynamic_content""segmentation" and "ai" are in both sets. Once you remove instances of these from each set, you are left without any other elements.
Take a look at $relations - I need a list of items that occur more than once in that array.
$unique is there only because I thought I could use it for symdiff comparison, it’s a by-product of $relations. It starts to feel like a dead-end, though.
I have another idea to test tomorrow at work: range $unique over $relations, and each iteration deletes the current value from $relations. If I’m thinking correctly, it will only delete the first occurrence it finds. This should leave me with an array of items that occur more than once. If an item is repeated three times, two of those will remain in my final array, and that’s fine for my usage scenario.
Another approach might be to iterate $relations over itself and count the occurence of each iterable value, save the value-occurences pairs into a map, and then output only the values with occurences > 1 from that map.