Thread overview
Finding duplicate elements
Aug 15, 2023
vino
Aug 15, 2023
kdevel
Aug 16, 2023
Ferhat Kurtulmuş
Aug 16, 2023
FeepingCreature
Aug 27, 2023
vino
August 15, 2023

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

August 15, 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

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"]
August 16, 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

Another approach would be using a rbtree as container at the first place.

August 16, 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);
}
August 27, 2023

On Wednesday, 16 August 2023 at 12:51:31 UTC, FeepingCreature wrote:

>

On Tuesday, 15 August 2023 at 17:59:27 UTC, vino wrote:

>

[...]

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);
}

Thank you very much