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

Line 2,088:
'XYZ ZYX' (7) -> 'X' (0x58) at 0, 6
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' (36) -> '0' (0x30) at 9, 24</pre>
 
=={{header|jq}}==
"First Duplicate" is here understood to be the first character found to be a duplicate
when scanning from left to right, so the First Duplicate in XYYX is Y. It would require only a trivial
modification of `firstDuplicate` as defined here to implement the alternative
interpretation as the first character to be duplicated.
<lang jq># Emit null if there is no duplicate, else [c, [ix1, ix2]]
def firstDuplicate:
label $out
| foreach explode[] as $i ({ix: -1};
.ix += 1
| .ix as $ix
| .iu = ([$i] | implode)
| .[.iu] += [ $ix] ;
if .[.iu]|length == 2 then [.iu, .[.iu]], break $out else empty end )
// null ;</lang>
 
Some helper functions for accomplishing other aspects of the task:
<lang jq>def hex:
def hx: [if . < 10 then 48 + . else 55 + . end] | implode ;
explode[0] | "\(./16 | floor | hx)\(. % 16 | hx)";
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def q: "«\(.)»";
 
def header:
"\("string"|q|lpad(38)) : |s| : C : hex IO=0";
 
def data:
"",
".",
"abcABC",
"XYZ ZYX",
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" ;</lang>
The main program:<lang jq>header,
(data
| firstDuplicate as [$k, $v]
| "\(q|lpad(38)) : \(length|lpad(4)) : \($k // " ") : \($k |if . then hex else " " end) \($v // [])" )
</lang>
{{out}}
<lang sh>
«string» : |s| : C : hex IO=0
«» : 0 : : []
«.» : 1 : : []
«abcABC» : 6 : : []
«XYZ ZYX» : 7 : Z : 5A [2,4]
«1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ» : 36 : 0 : 30 [9,24]</lang>
 
=={{header|Julia}}==
2,451

edits