Skip to content

Instantly share code, notes, and snippets.

@pppillai
Created May 13, 2020 18:40
Show Gist options
  • Save pppillai/45617c7f3ec3c8094c43af7587a192e6 to your computer and use it in GitHub Desktop.
Save pppillai/45617c7f3ec3c8094c43af7587a192e6 to your computer and use it in GitHub Desktop.
-module(countchar).
-export([count_characters/1]).
count_characters(Str) ->
count_characters(Str, #{}).
count_characters([], Result) ->
Result;
count_characters([H|T], Result) ->
case maps:is_key(H, Result) of
true ->
%io:format("In True : ~p~n", [H]),
{ok, Value} = maps:find(H, Result),
NewResult = Result#{H := Value + 1};
false ->
%io:format("In False : ~p~n", [H]),
NewResult = maps:put(H, 1, Result)
end,
count_characters(T, NewResult).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment