Thread overview
Simplest way to convert an array into a set
Feb 13, 2023
Matt
Feb 13, 2023
Christian Köstlin
Feb 13, 2023
H. S. Teoh
Feb 13, 2023
ProtectAndHide
Feb 14, 2023
Salih Dincer
February 13, 2023

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

February 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 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

February 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 course
You are looking probably for https://dlang.org/library/std/algorithm/iteration/uniq.html.


Kind regards,
Christian

February 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 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
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

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:

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:

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