Finding duplicate elements

FeepingCreature feepingcreature at gmail.com
Wed Aug 16 12:51:31 UTC 2023


On Tuesday, 15 August 2023 at 17:59:27 UTC, vino wrote:
> Hi All,
>
>  Request your help in finding duplicate element without sorting 
> as per the below example
>
> ```
> Example:
> string[] args = [" test3", "test2 ", " test1 ", " test1 ", " "];
>
> Output Required:
> If duplicate element found then print "Duplicate element found: 
> <element name>"
> else print ["test3", "test2", "test1"];
> ```
> From,
> Vino.B

```
import std;
void main() {
     string[] args = [" test3", "test2 ", " test1 ", " test1 ", " 
"];
     findDuplicates(args);
}
void findDuplicates(string[] args) {
     bool[string] dupes;
     foreach (arg; args) {
         if (arg in dupes) {
             writefln!"Duplicate element found: %s"(arg);
             return;
         }
         dupes[arg] = true;
     }
     writefln!"%s"(dupes.keys);
}
```



More information about the Digitalmars-d-learn mailing list