Skip to content

Commit 07c43cb

Browse files
Add Duplicate Character Counter in Lua (#4518)
1 parent 5d6a5af commit 07c43cb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function duplicateCharacterCounter(word)
2+
local hasDuplicates = false;
3+
4+
-- Empty or no input validation
5+
if word == "" or word == nil then
6+
print("Usage: please provide a string")
7+
return
8+
9+
-- Check for duplicates validation
10+
elseif (string.len(word) >= 1) then
11+
local myTable = {}
12+
local duplicateList = {}
13+
14+
for i=1, string.len(word) do
15+
local char = string.sub(word, i, i)
16+
if (myTable[char]) then
17+
myTable[char] = myTable[char]+1
18+
hasDuplicates = true
19+
20+
else
21+
myTable[char] = 1;
22+
table.insert(duplicateList, char)
23+
end
24+
end
25+
for _, char in ipairs(duplicateList) do
26+
if myTable[char] > 1 then
27+
print(char .. ": " .. myTable[char])
28+
end
29+
end
30+
end
31+
32+
-- No duplicates validation
33+
if not (hasDuplicates) then
34+
print("No duplicate characters")
35+
end
36+
end
37+
38+
duplicateCharacterCounter(arg[1])

0 commit comments

Comments
 (0)