www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Simplest way to convert an array into a set

reply Matt <webwraith fastmail.fm> writes:
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
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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 `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
prev sibling next sibling parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
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
Feb 13 2023
prev sibling next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
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 -- Маленькие детки - маленькие бедки.
Feb 13 2023
prev sibling next sibling parent ProtectAndHide <ProtectAndHide gmail.com> writes:
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]
Feb 13 2023
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
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