Sets (in the computering sense) are a useful thing, but they're a
"new" (post-C) thing and so the syntax isn't standardised across
languages the way it is for e.g. arrays. (In particular, they aren't
available as standard in Perl, which has been my language of choice
for many years.) So I thought I'd look at the way they work.
Conceptually they can be seen as a sort of second-class hash
(dict, associative array): they have a list of keys, with no
duplications, but they don't have values. So for example an easy way
to remove duplicates from a list is to turn it into a set, then (since
this will usually lose the order they were in) if necessary turn that
set's keys back into a list and put them into whatever order is
wanted.
In Rust they're std::collections::HashSet and the syntax is very
similar to HashMap from the same library.
let mut h = HashSet::new()
h.insert(v);
h.remove(&v);
if h.contains(&v) { … }
What one can also do with a set, which makes less sense with a hash,
is operations on the keys of two sets: equivalence, intersection (AND,
key is in both), union (OR, key is in either or both), symmetric
difference (XOR, key is in just one), difference (keys in A but not in
B, A XOR (A AND B)), is-subset (A AND B == A), etc. In Rust again:
let diff: HashSet<_> = a.difference(&b).collect();
let inter: HashSet<_> = a.intersection(&b).collect();
etc.
JavaScript has a Set that has syntax like a hash. Python has a set
similarly.
In Ruby one has to require 'set' but it's a core module.
Intersection overloads the & operator. In Crystal, no need for the
require but the syntax is the same.
In Raku there's a Set (immutable) and a SetHash (mutable), and
intersection is (&).
In Kotlin and Scala they're the Set type (Scala gets strange about
making them mutable, because really it doesn't like mutable variables
at all).
Some languages don't have sets in the core at all (Perl, Lua,
PostScript, Typst) but they can be faked by using hashes/dicts and
ignoring the values. One does need to write the special extra
operations of course. (My PostScript libraries include this.)
One can also extend this concept to a multiset or counter, in which
the value for a key is the number of occurrences of that key in the
overall set of data. (Which is trivially also a hash, but with its
usage restricted in certain ways.) That's even less likely to be a
core language feature, though Python has a Counter library.