Finding duplicate elements

kdevel kdevel at vogtner.de
Tue Aug 15 18:28:48 UTC 2023


On Tuesday, 15 August 2023 at 17:59:27 UTC, vino wrote:
>  Request your help in finding duplicate element without sorting 
> as per the below example

```d
module remove_duplicates;
import std.stdio;
import std.format : format;
import std.string : strip;

int main (string [] args)
{
    bool [string] found;
    string [] result;
    foreach (s; args [1..$]) {
       auto t = s.strip;
       if (t == "")
          continue;
       if (t in found)
          stderr.writefln!"Duplicate element found: <%s>" 
("element name");
       else {
          found [t] = true;
          result ~= t;
       }
    }
    result.writeln;
    return 0;
}
```

```
$ ./remove-duplicates " test3" "test2 " " test1 " " test1 " " "
Duplicate element found: <element name>
["test3", "test2", "test1"]
```


More information about the Digitalmars-d-learn mailing list