I need to write a function that takes a string and returns it with duplicate characters removed in Lua. What I need help with is…
- Making a hash with letters and the counts
- Making each letter equal to only one, deleting more than one occurrence
- Converting hash into the new string with duplicates removed
A simple function/algorithm would be appreciated!
If you only need one instance of each character then you probably don’t need to keep track of the counts; you can compare the input string against the same table you use to generate the output.
This will probably be slower than the function below for short strings, but it’s generally best to avoid excessive string concatenation in Lua, for the reasons outlined here. If you’re sure the output string will be fairly short, you can get rid of
contains()and use this: