Sets
Also known as: unique distinct duplicates hashset membership contains deduplicate
● Beginner3 minBefore this: ListsSpec 2.2
A set holds each value once. Adding a value it already has changes nothing.
fn main() {
const visits = ["asha", "ravi", "asha", "mira", "ravi"]
let people = Set.of()
for name in visits {
people.add(name)
}
print(people.len())
print(people.to_list())
}The program ran and printed:
kavach run dedupe.kav
3["asha", "ravi", "mira"]
Five visits, three people: the set kept each name once, in the order first seen. Use a set to remove duplicates, or to ask quickly whether you have seen something.