digitalmars.D.learn - Simplest way to convert an array into a set
- Matt (7/7) Feb 13 2023 Obviously, there is no "set" object in D, but I was wondering
- Steven Schveighoffer (7/13) Feb 13 2023 If you sort, you can use the `std.algorithm.iteration.uniq` function to
- =?UTF-8?Q?Christian_K=c3=b6stlin?= (5/11) Feb 13 2023 You are looking probably for
- H. S. Teoh (29/36) Feb 13 2023 Actually, bool[T] could be used as a set object of sorts. Or even
- ProtectAndHide (6/13) Feb 13 2023 int[] someArray = [5, 3, 8, 5, 2, 3, 0, 8];
- Salih Dincer (31/34) Feb 13 2023 Where did you find out that there is no set() in the D
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
Feb 13 2023
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 courseIf you sort, you can use the `std.algorithm.iteration.uniq` function to get that information. I don't believe there's a specific method to remove duplicates in place. Also, although there isn't a set type, you can use `bool[T]` as a crude set type. -Steve
Feb 13 2023
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 courseYou are looking probably for https://dlang.org/library/std/algorithm/iteration/uniq.html. Kind regards, Christian
Feb 13 2023
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 courseTry 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 -- Маленькие детки - маленькие бедки.
Feb 13 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 courseint[] 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]
Feb 13 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 79
Feb 13 2023