Determine if a string has all unique characters: Difference between revisions
Determine if a string has all unique characters (view source)
Revision as of 16:04, 30 December 2019
, 4 years ago→{{header|JavaScript}}: Updated output of group and sort example, used sortOn in lieu of sortBy
(→{{header|JavaScript}}: Updated alternative to grouping and sorting) |
m (→{{header|JavaScript}}: Updated output of group and sort example, used sortOn in lieu of sortBy) |
||
Line 569:
duplicates = filter(g => 1 < g.length)(
groupBy(on(eq)(snd))(
zip(enumFrom(0))(chars(s))
)
Line 576:
return 0 < duplicates.length ? Just(
fanArrow(compose(snd, fst))(map(fst))(
duplicates
)[0]
Line 587:
console.log(
fTable('First duplicated character, if any:')(
s => `'${s}' (${s.length})`
)(maybe('None')(tpl => {
const [c, ixs] = Array.from(tpl);
Line 623:
// chars :: String -> [Char]
const chars = s => s.split('');
x => y => {▼
const▼
a = f(x),▼
b = f(y);▼
return a < b ? -1 : (a > b ? 1 : 0);▼
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 730 ⟶ 721:
const snd = tpl => tpl[1];
//
const
// Equivalent to sortBy(comparing(f)), but with f(x)
// evaluated only once for each x in xs.
// ('Schwartzian' decorate-sort-undecorate).
// take :: Int -> [a] -> [a]
Line 775 ⟶ 772:
{{Out}}
<pre>First duplicated character, if any:
'' (0) -> None
'.' (1) -> None
'abcABC' (6) -> None
'XYZ ZYX' (7) -> 'X' (0x58) at 0, 6
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' (36) -> '0' (0x30) at 9, 24</pre>
|