Discovering the Elegance of Removing Duplicates with Rosetta Code

Rosetta Code (https://rosettacode.org/) is a website which presents “solutions to the same task in as many different languages as possible (…)”. Although programming languages have similar and different approaches in solving problems, the site could be inspiring for you, as it is for me. Read on!
Last week, I was looking -just for fun – for a solution to a relatively small problem: how can I -within VisualNeoWin- remove duplicate characters in a string? I found several solutions myself, but one solution intrigued me:

SetVar "[str]" "aaxaabbeedxgg"
SetVar "[temp]" "[str]"
SetVar "[uniques]" ""

StrLen "[str]" "[len_str]"

.iterate over all characters of [str]
Loop "1" "[len_str]" "[i]"

.assign a char of [str] to the variable [char]
 SubStr "[str]" "[i]" "1" "[char]"

.check if this character can be found in [temp], which is a copy of [str]
 SearchStr "[char]" "[temp]" "[found]" ""

. if [char] is found, remove all instances of [char] from [temp] and add [char] to the string [uniques]
. iteration 1: the first character 'a' of [str] is found in [temp], all characters 'a' are removed from [temp] (1) and the character 'a' is added to [uniques]
. iteration 2: the second character of [str], again 'a', will not be found in the modified [temp] and [uniques] will not be changed
. iteration 3: the third character 'x' of [str] is found in [temp], all characters 'x' are removed from [temp] (1) and the character 'x' is added to [uniques]
. etc.

 If "[found]" ">" "0"
  StrReplace "[temp]" "[char]" "" "[temp]" ""
  SetVar "[uniques]" "[uniques][char]"
 EndIf

EndLoop

.result: [uniques] contains the value 'axbedg'

However, this solution seemed to me not completely efficient. I got the idea to look at solutions on Rosetta Code (why reinvent the wheel?). I’ve to say that I was impressed again by the diversity of the given solutions, from simple to complex, from short to long, from built-in functions to extensive constructs etc. Then I found this:

https://rosettacode.org/wiki/Remove_duplicate_elements#CoffeeScript

data = [ 1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d" ]
set = []
set.push i for i in data when not (i in set)

This code says (translated to our case):

put a character into [uniques] if the character is not found in [uniques]

WOW!!! The same idea as I had, but much better! It is more efficient, shorter and faster: [temp] and StrReplace could be removed.

SetVar "[str]" "aaxaabbeedxgg"
SetVar "[uniques]" ""

StrLen "[str]" "[len_str]"
Loop "1" "[len_str]" "[i]"

 SubStr "[str]" "[i]" "1" "[char]"

 SearchStr "[char]" "[uniques]" "[found]" ""
 If "[found]" "=" "0"
  SetVar "[uniques]" "[uniques][char]"
 EndIf

EndLoop

.result: [uniques] contains the value 'axbedg'

Lifelong learning! And Rosetta helps…Thanks for reading.

Best regards,
Reinier Maliepaard

Footnote
(1) Notice that I do not change the original [str]. The loop could be confused: [len_str] is the ‘stop value’ of [str] and should be redefined after modifications of [str]… Try it yourself, and you’ll be surprised by the unexpected results.