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

(Added Algol 68)
Line 1,320:
"XYZ ZYX" (7) -> 'X' (0x58) at 0, 6
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36) -> '0' (0x30) at 9, 24</pre>
 
=={{header|J}}==
Quotes surround the literals to make the computed one-at-a-time results present well in the combined table.
<lang j>
rc_unique=: monad define
string=. '"' , y , '"'
self_classification=. = y NB. deprecated- consumes space proportional to the squared tally of y (*: # y)
is_unique=. self_classification =&# y
if. is_unique do.
(# y) ; string ; 'unique'
else.
duplicate_masks=. (#~ (1 < +/"1)) self_classification
duplicate_characters=. ~. y #~ +./ duplicate_masks
ASCII_values_of_duplicates=. a. i. duplicate_characters
markers=. duplicate_masks { ' ^'
A=. (# y) ; string , ' ' ,. markers
B=. 'duplicate' , ASCII_values_of_duplicates ('<' , (#~ 31&<)~ , '> ASCII ' , ":@:[)"0 duplicate_characters
A , < B
end.
)
</lang>
Tests include those of the C example and a pair of MS-DOS line terminations.
<pre>
(;:'length string analysis') , rc_unique;._2';.;abcABC;XYZ YZX;1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ; ;2;333;.55;tttTTT;4444 444k;',(4$CRLF),';'
┌──────┬──────────────────────────────────────┬─────────────┐
│length│string │analysis │
├──────┼──────────────────────────────────────┼─────────────┤
│0 │"" │unique │
├──────┼──────────────────────────────────────┼─────────────┤
│1 │"." │unique │
├──────┼──────────────────────────────────────┼─────────────┤
│6 │"abcABC" │unique │
├──────┼──────────────────────────────────────┼─────────────┤
│7 │"XYZ YZX" │duplicate │
│ │ ^ ^ │<X> ASCII 88 │
│ │ ^ ^ │<Y> ASCII 89 │
│ │ ^ ^ │<Z> ASCII 90 │
├──────┼──────────────────────────────────────┼─────────────┤
│36 │"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"│duplicate │
│ │ ^ ^ │<0> ASCII 48 │
├──────┼──────────────────────────────────────┼─────────────┤
│3 │" " │duplicate │
│ │ ^^^ │< > ASCII 32 │
├──────┼──────────────────────────────────────┼─────────────┤
│1 │"2" │unique │
├──────┼──────────────────────────────────────┼─────────────┤
│3 │"333" │duplicate │
│ │ ^^^ │<3> ASCII 51 │
├──────┼──────────────────────────────────────┼─────────────┤
│3 │".55" │duplicate │
│ │ ^^ │<5> ASCII 53 │
├──────┼──────────────────────────────────────┼─────────────┤
│6 │"tttTTT" │duplicate │
│ │ ^^^ │<t> ASCII 116│
│ │ ^^^ │<T> ASCII 84 │
├──────┼──────────────────────────────────────┼─────────────┤
│9 │"4444 444k" │duplicate │
│ │ ^^^^ ^^^ │<4> ASCII 52 │
├──────┼──────────────────────────────────────┼─────────────┤
│4 │" " │duplicate │
│ │ ^ ^ │<> ASCII 13 │
│ │ ^ ^ │<> ASCII 10 │
└──────┴──────────────────────────────────────┴─────────────┘
</pre>
 
More uniqueness tests with performance comparison
<lang j>
NB. unique_index answers "Do the left and right indexes match?"
unique_index=: (i. -: i:)~
assert 0 1 -: 2 unique_index\0 0 1
 
NB. unique_set answers "Are lengths of the nub and original equal?"
unique_set=: -:&# ~.
assert 0 1 -: _2 unique_set\'aab'
 
NB. unique_nubsieve answers "Are the items unique?"
unique_nubsieve=: 0 -.@:e. ~:
assert 0 1 -: _2 unique_nubsieve\'aab'
 
Note'compared to nubsieve'
the index method takes 131% longer and 15 times additional memory
the set formation method 15% longer and uses 7 times additional memory.
)
</lang>
 
=={{header|Java}}==
Anonymous user