Skip to content

Commit 53222b6

Browse files
committed
Add Duplicate Character Counter in Elixir
1 parent 03543e9 commit 53222b6

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
case System.argv() do
2+
[str] when byte_size(str) > 0 ->
3+
chars = String.graphemes(str)
4+
5+
frequencies = Enum.reduce(chars, %{}, fn el, acc ->
6+
Map.update(acc, el, 1, &(&1 + 1))
7+
end) |> Enum.filter(fn {_, count} -> count > 1 end) |> Map.new()
8+
9+
if Enum.empty?(frequencies) do
10+
IO.puts("No duplicate characters")
11+
else
12+
chars
13+
|> Enum.uniq()
14+
|> Enum.each(fn char ->
15+
if Map.has_key?(frequencies, char) do
16+
IO.puts("#{char}: #{frequencies[char]}")
17+
end
18+
end)
19+
end
20+
_ -> IO.puts("Usage: please provide a string")
21+
end

0 commit comments

Comments
 (0)