Simplest way to convert an array into a set
Salih Dincer
salihdb at hotmail.com
Tue Feb 14 01:26:16 UTC 2023
On Monday, 13 February 2023 at 18:04:40 UTC, Matt wrote:
> Obviously, there is no "set" object in D, but I was wondering
> what the quickest way to remove duplicates from an array would
> be...
Where did you find out that there is no set() in the D
programming language?
**Simple example:**
```d
import std;
auto str = "D Programming Language";
void main()
{
size_t i;
str.map!(n => tuple(n, i++))
.assocArray.keys
.sort.writeln; // DLPaegimnoru
}
```
**X-Ray example:**
```d
auto set(R)(R[] list) {
size_t[R] result;
foreach(i, item; list) {
result[item] = i;
}
return result.keys;
}
unittest
{
auto xRay = set(str.dup);
xRay.array.sort.writeln; // DLPaegimnoru
}
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list