Determine if a string has all unique characters: Difference between revisions

Content added Content deleted
m (Reformatted to reduce line count)
(Realize in F#)
Line 926: Line 926:
Character '0' (hex 30) occurs at positions 10 and 25.</pre>
Character '0' (hex 30) occurs at positions 10 and 25.</pre>


=={{header|F_Sharp|F#}}==
<lang fsharp>
// Determine if a string has all unique characters. Nigel Galloway: June 9th., 2020
let fN (n:string)=n.ToCharArray()|>Array.mapi(fun n g->(n,g))|>Array.groupBy(fun (_,n)->n)|>Array.filter(fun(_,n)->n.Length>1)

let allUnique n=match fN n with
g when g.Length=0->printfn "All charcters in <<<%s>>> (length %d) are unique" n n.Length
|g->Array.iter(fun(n,g)->printf "%A is repeated at positions" n; Array.iter(fun(n,_)->printf " %d" n)g;printf " ")g
printfn "in <<<%s>>> (length %d)" n n.Length

allUnique ""
allUnique "."
allUnique "abcABC"
allUnique "XYZ ZYX"
allUnique "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"
</lang>
{{out}}
<pre>
All charcters in <<<>>> (length 0) are unique
All charcters in <<<.>>> (length 1) are unique
All charcters in <<<abcABC>>> (length 6) are unique
'X' is repeated at positions 0 6 'Y' is repeated at positions 1 5 'Z' is repeated at positions 2 4 in <<<XYZ ZYX>>> (length 7)
'0' is repeated at positions 9 24 in <<<1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ>>> (length 36)
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting fry generalizations io kernel math.parser
<lang factor>USING: formatting fry generalizations io kernel math.parser