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
Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 13, 2023 Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
February 13, 2023 Re: Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | On 2/13/23 1:04 PM, 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 If you sort, you can use the Also, although there isn't a set type, you can use -Steve |
February 13, 2023 Re: Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | On 13.02.23 19:04, 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 You are looking probably for https://dlang.org/library/std/algorithm/iteration/uniq.html. Kind regards, Christian |
February 13, 2023 Re: Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | On Mon, Feb 13, 2023 at 06:04:40PM +0000, Matt via Digitalmars-d-learn wrote: > Obviously, there is no "set" object in D, Actually, bool[T] could be used as a set object of sorts. Or even void[0][T], though that's a little more verbose to type. But this can be aliased to something nicer (see below). > 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 Try this: -------------------------snip------------------------- import std; auto deduplicate(R)(R input) if (isInputRange!R) { alias Unit = void[0]; enum unit = Unit.init; Unit[ElementType!R] seen; return input.filter!((e) { if (e in seen) return false; seen[e] = unit; return true; }); } unittest { assert([ 1, 2, 3, 4, 2, 5, 6, 4, 7 ].deduplicate.array == [ 1, 2, 3, 4, 5, 6, 7 ]); assert([ "abc", "def", "def", "ghi", "abc", "jkl" ].deduplicate.array == [ "abc", "def", "ghi", "jkl" ]); } -------------------------snip------------------------- T -- Маленькие детки - маленькие бедки. |
February 13, 2023 Re: Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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]
|
February 14, 2023 Re: Simplest way to convert an array into a set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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:
X-Ray example:
SDB@79 |