I would like to develope a method to extract a list of unique values from vector:
let v = ["a","b","c","c"];
unique(v) // it should return ["a","b","c"];
Any suggestion on how I can accomplish the extraction of a list of unique values?
I would like to develope a method to extract a list of unique values from vector:
let v = ["a","b","c","c"];
unique(v) // it should return ["a","b","c"];
Any suggestion on how I can accomplish the extraction of a list of unique values?
This library GitHub - ZhenyaUsenko/motoko-hash-map: Stable hash maps for Motoko has a Set which may suite your needs unless you actually need to keep the unique entries around. (or you can use the fromIterof Set to import the .vals of your Vec if not).
Thank you for the indications. I finally uised TrieSet
, fromArray
and a hash funtion for text object from Zheny Usenko git project:
func hashText(key: Text): Nat32 {
Prim.hashBlob(Prim.encodeUtf8(key)) & 0x3fffffff;
};
let set2 = TrieSet.fromArray<Text>(["1", "2", "3", "1", "2", "3", "1"], hashText, Text.equal);
let vals2 = TrieSet.toArray(set2);
for (element in vals2.vals()) {
Debug.print("val:" # element);
};