Simplest way to convert an array into a set

ProtectAndHide ProtectAndHide at gmail.com
Mon Feb 13 20:39:22 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. I was convinced I'd seen a "unique" method somewhere, but 
> I've looked through the documentation for std.array, 
> std.algorithm AND std.range, and I've either missed it, or my 
> memory is playing tricks on me. That, or I'm looking in the 
> wrong place entirely, of course

int[] someArray = [5, 3, 8, 5, 2, 3, 0, 8];
int[] uniqueArray;
uniqueArray = uniq(sort(someArray)).array; // take note of the 
sort
writeln(uniqueArray); // [0, 2, 3, 5, 8]



More information about the Digitalmars-d-learn mailing list