Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Rust
- Clojure
- C++
- C++
- C#
- D
- D
- Dart
- Elixir
- Elixir
- Erlang
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Perl
- Python
- Python
- Ruby
- Scala
- Smalltalk
(def y (set x))
bool[typeof(x[0])] y;
foreach (e ; x)
y[e] = true;
D doesn't have a set type, use an associative array.
You may replace typeof(x[0]) with the named type of the elements.
You may replace typeof(x[0]) with the named type of the elements.
y = x |> Enum.uniq |> List.to_tuple
Remove list duplication and convert to a tuple.
Warning: a tuple is not really a set.
Warning: a tuple is not really a set.
y = MapSet.new(x)
Y = sets:from_list(X).
func sliceToSet[T comparable](x []T) map[T]struct{} {
y := make(map[T]struct{}, len(x))
for _, v := range x {
y[v] = struct{}{}
}
return y
}
sliceToSet is generic. Its type parameter T has a constraint: must be comparable with ==.
y := make(map[T]struct{}, len(x))
for _, v := range x {
y[v] = struct{}{}
}
Iterate to add each item to the map.
T is the type of the items.
T is the type of the items.
Set<T> y = x.stream().collect(toSet());
Set<T> y = new HashSet<>(x);
T is the type of the elements.
Set<T> y = new LinkedHashSet<>(x);
The `LinkedHashSet` will preserve the order of x.
(setf y (remove-duplicates x))
local hash = {}
local y = {}
for _,v in ipairs(x) do
if (not hash[v]) then
y[#y+1] = v
hash[v] = true
end
end
for i := Low(X) to High(X) do Include(Y,X[i]);
Sets never have repeated values in Pascal. Adding a value that already is in the set leaves the set unchanged (and does not raise any error).
my %y = map {$_=>0} @x;
Convert a list to a hash, the value is unimportant, so we used 0.
my @y = uniq @x;
Filters a list of values to remove subsequent duplicates. Preserves the order of unique elements, and retains the first value of any duplicate set.
y = {*x}
val y = x.toSet
The default implementation of Set is a HashSet
y := x asSet