Unique characters: Difference between revisions
Content added Content deleted
(→{{header|JavaScript}}: Added a variant which folds the strings to a hash of character frequencies) |
|||
Line 633: | Line 633: | ||
{{Out}} |
{{Out}} |
||
<pre>["1","5","6","b","g","s","t","z"]</pre> |
<pre>["1","5","6","b","g","s","t","z"]</pre> |
||
Or, folding the strings (with Array.reduce) down to a hash of character frequencies: |
|||
<lang javascript>(() => { |
|||
"use strict"; |
|||
// uniqueChars :: [String] -> [Char] |
|||
const uniqueChars = ws => |
|||
Object.entries( |
|||
ws.reduce( |
|||
(dict, w) => [...w].reduce( |
|||
(a, c) => Object.assign({}, a, { |
|||
[c]: 1 + (a[c] || 0) |
|||
}), |
|||
dict |
|||
), {} |
|||
) |
|||
) |
|||
.flatMap( |
|||
([k, v]) => 1 === v ? ( |
|||
[k] |
|||
) : [] |
|||
); |
|||
// ---------------------- TEST ----------------------- |
|||
const main = () => |
|||
uniqueChars([ |
|||
"133252abcdeeffd", "a6789798st", "yxcdfgxcyz" |
|||
]); |
|||
return JSON.stringify(main()); |
|||
})();</lang> |
|||
{{Out}} |
|||
<pre>["1","5","6","b","s","t","g","z"]</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |