Programmers are usually the kind of people who like to have everything organized—after all, a large part of programming is organizing data structures in programs. Consider a list that has many elements—and some of them may be repeated or duplicates. Programming languages often offer built-in methods to eliminate the duplicates.
In the C# language, we have a method called Distinct
that is part of System.Linq
. When called, it eliminates duplicates in an IEnumerable
and returns the result—we may need to call ToList
to get back to a List
.
In Rust, we have a dedup
function on the Vector
type. This requires that the vector be sorted (or in groups of equal elements), or it will not work correctly—it works by looping through the elements and checking to see if there are any duplicates next to each other. If you are going to rewrite it in Rust, remember dedup
.
In Go, we have a function in the slices package called slices.Compact
. This works the same as the Rust dedup
function—it removes repeated elements in a sorted slice. The slice must be sorted (or have same elements next to each other somehow).