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

m
(→‎{{header|jq}}: simplify)
m (→‎{{header|Wren}}: Minor tidy)
 
(12 intermediate revisions by 7 users not shown)
Line 775:
</pre>
 
=={{header|BQNBASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">subroutine CaracteresUnicos (cad$)
lngt = length(cad$)
print 'Cadena = "'; cad$; '", longitud = '; lngt
for i = 1 to lngt
for j = i + 1 to lngt
if mid(cad$,i,1) = mid(cad$,j,1) then
print " Primer duplicado en las posiciones " & i & " y " & j & ", caracter = '" & mid(cad$,i,1) & "', valor hex = " & tohex(asc(mid(cad$,i,1)))
return
end if
next j
next i
print " Todos los caracteres son unicos." & chr(10)
end subroutine
 
call CaracteresUnicos("")
call CaracteresUnicos(".")
call CaracteresUnicos("abcABC")
call CaracteresUnicos("XYZ ZYX")
call CaracteresUnicos("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")</syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 sub caracteresunicos(cad$)
120 lngt = len(cad$)
130 print 'Cadena = "';cad$;'" longitud = ';lngt
140 for i = 1 to lngt
150 for j = i+1 to lngt
160 if mid$(cad$,i,1) = mid$(cad$,j,1) then
170 print " Primer duplicado en las posiciones ";i;" y ";j;", caracter = '";mid$(cad$,i,1);"', valor hex = ";hex$(asc(mid$(cad$,i,1)))
180 print
190 exit sub
200 endif
210 next j
220 next i
230 print " Todos los caracteres son unicos.";chr$(10)
240 end sub
250 caracteresunicos("")
260 caracteresunicos(".")
270 caracteresunicos("abcABC")
280 caracteresunicos("XYZ ZYX")
290 caracteresunicos("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")
300 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Sub CaracteresUnicos (cad As String)
Dim As Integer lngt = Len(cad)
Print "Cadena = """; cad; """, longitud = "; lngt
For i As Integer = 1 To lngt
For j As Integer = i + 1 To lngt
If Mid(cad,i,1) = Mid(cad,j,1) Then
Print " Primer duplicado en las posiciones " & i & _
" y " & j & ", caracter = '" & Mid(cad,i,1) & _
"', valor hex = " & Hex(Asc(Mid(cad,i,1)))
Print
Exit Sub
End If
Next j
Next i
Print " Todos los caracteres son unicos." & Chr(10)
End Sub
 
CaracteresUnicos ("")
CaracteresUnicos (".")
CaracteresUnicos ("abcABC")
CaracteresUnicos ("XYZ ZYX")
CaracteresUnicos ("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")
Sleep</syntaxhighlight>
{{out}}
<pre>
Cadena = "", longitud = 0
Todos los caracteres son unicos.
 
Cadena = ".", longitud = 1
Todos los caracteres son unicos.
 
Cadena = "abcABC", longitud = 6
Todos los caracteres son unicos.
 
Cadena = "XYZ ZYX", longitud = 7
Primer duplicado en las posiciones 1 y 7, caracter = 'X', valor hex = 58
 
Cadena = "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ", longitud = 36
Primer duplicado en las posiciones 10 y 25, caracter = '0', valor hex = 30
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebsic">void local fn StringHasUniqueCharacters( string as CFStringRef )
long i, j, length = len( string )
if length == 0 then printf @"The string \"\" is empty and thus has no characters to compare.\n" : exit fn
printf @"The string: \"%@\" has %ld characters.", string, length
for i = 0 to length - 1
for j = i + 1 to length - 1
if ( fn StringIsEqual( mid( string, i, 1 ), mid( string, j, 1 ) ) )
CFStringRef duplicate = mid( string, i, 1 )
printf @"The first duplicate character, \"%@\", is found at positions %ld and %ld.", duplicate, i, j
printf @"The hex value of \"%@\" is: 0X%x\n", duplicate, fn StringCharacterAtIndex( duplicate, 0 )
exit fn
end if
next
next
printf @"All characters in string are unique.\n"
end fn
 
fn StringHasUniqueCharacters( @"" )
fn StringHasUniqueCharacters( @"." )
fn StringHasUniqueCharacters( @"abcABC" )
fn StringHasUniqueCharacters( @"XYZ ZYX" )
fn StringHasUniqueCharacters( @"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" )
 
HandleEvents</syntaxhighlight>
{{output}}
<pre>
The string "" is empty and thus has no characters to compare.
 
The string: "." has 1 characters.
All characters in string are unique.
 
The string: "abcABC" has 6 characters.
All characters in string are unique.
 
The string: "XYZ ZYX" has 7 characters.
The first duplicate character, "X", is found at positions 0 and 6.
The hex value of "X" is: 0X58
 
The string: "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" has 36 characters.
The first duplicate character, "0", is found at positions 9 and 24.
The hex value of "0" is: 0X30
</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Procedure CaracteresUnicos(cad.s)
lngt.i = Len(cad)
PrintN("Cadena = '" + cad + "' longitud = " + Str(lngt))
For i.i = 1 To lngt
For j.i = i + 1 To lngt
If Mid(cad, i, 1) = Mid(cad, j, 1)
PrintN(" Primer duplicado en las posiciones " + Str(i) + " y " + Str(j) + ", caracter = '" + Mid(cad, i, 1) + "', valor hex = " + Hex(Asc(Mid(cad, i, 1))))
ProcedureReturn
EndIf
Next
Next
PrintN(" Todos los caracteres son unicos.")
EndProcedure
 
OpenConsole()
CaracteresUnicos("")
CaracteresUnicos(".")
CaracteresUnicos("abcABC")
CaracteresUnicos("XYZ ZYX")
CaracteresUnicos("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")
PrintN(#CRLF$ + "--- Press ENTER to exit ---"): Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Similar as FreeBASIC entry.</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
Dim input() = {"", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"}
For Each s In input
Console.WriteLine($"'{s}' (Length {s.Length}) " + String.Join(", ", s.Select(Function(c, i) (c, i)).GroupBy(Function(t) t.c).Where(Function(g) g.Count() > 1).Select(Function(g) $"'{g.Key}' (0X{AscW(g.Key):X})[{String.Join(", ", g.Select(Function(t) t.i))}]").DefaultIfEmpty("All characters are unique.")))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>'' (Length 0) All characters are unique.
'.' (Length 1) All characters are unique.
'abcABC' (Length 6) All characters are unique.
'XYZ ZYX' (Length 7) 'X' (0X58)[0, 6], 'Y' (0X59)[1, 5], 'Z' (0X5A)[2, 4]
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' (Length 36) '0' (0X30)[9, 24]</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">sub caracteresunicos (cad$)
local lngt, i, j
 
lngt = len(cad$)
print "cadena = \"", cad$, "\", longitud = ", lngt
for i = 1 to lngt
for j = i + 1 to lngt
if mid$(cad$,i,1) = mid$(cad$,j,1) then
print " Primer duplicado en las posiciones ", i, " y ", j, ", caracter = \'", mid$(cad$,i,1), "\', valor hex = ", hex$(asc(mid$(cad$,i,1)))
print
return
end if
next j
next i
print " Todos los caracteres son unicos.\n"
end sub
 
caracteresunicos ("")
caracteresunicos (".")
caracteresunicos ("abcABC")
caracteresunicos ("XYZ ZYX")
caracteresunicos ("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
 
=={{header|BQN}}==
O(n^2) method used for finding indices.
 
Hex function and loop similar to [[Determine if a string has all the same characters#BQN|Determine if a string has all the same characters]]
 
 
<syntaxhighlight lang="bqn">Check←=⌜˜
Line 1,247 ⟶ 1,460:
String contains a repeated character.
Character "0" (hex 0030) occurs at positions 11 and 26.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ hex h .
for d in [ h div 16 h mod 16 ]
if d > 9
d += 7
.
h$ &= strchar (d + 48)
.
return h$
.
proc unichar s$ . .
len d[] 65536
s$[] = strchars s$
for i to len s$[]
h = strcode s$[i]
if d[h] <> 0
write " --> duplicates: '" & s$[i] & "' (" & hex h & "h)"
print "' positions: " & d[h] & ", " & i
return
.
d[h] = i
.
print "ok"
.
repeat
s$ = input
until s$ = "EOF"
print "'" & s$ & "'" & " length " & len s$
unichar s$
print ""
.
input_data
 
.
abcABC
XYZ ZYX
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ
EOF
</syntaxhighlight>
 
=={{header|Erlang|Erlang}}==
Line 1,435 ⟶ 1,689:
 
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Sub CaracteresUnicos (cad As String)
Dim As Integer lngt = Len(cad)
Print "Cadena = """; cad; """, longitud = "; lngt
For i As Integer = 1 To lngt
For j As Integer = i + 1 To lngt
If Mid(cad,i,1) = Mid(cad,j,1) Then
Print " Primer duplicado en las posiciones " & i & _
" y " & j & ", caracter = '" & Mid(cad,i,1) & _
"', valor hex = " & Hex(Asc(Mid(cad,i,1)))
Print
Exit Sub
End If
Next j
Next i
Print " Todos los caracteres son unicos." & Chr(10)
End Sub
 
CaracteresUnicos ("")
CaracteresUnicos (".")
CaracteresUnicos ("abcABC")
CaracteresUnicos ("XYZ ZYX")
CaracteresUnicos ("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")
Sleep</syntaxhighlight>
{{out}}
<pre>
Cadena = "", longitud = 0
Todos los caracteres son unicos.
 
Cadena = ".", longitud = 1
Todos los caracteres son unicos.
 
Cadena = "abcABC", longitud = 6
Todos los caracteres son unicos.
 
Cadena = "XYZ ZYX", longitud = 7
Primer duplicado en las posiciones 1 y 7, caracter = 'X', valor hex = 58
 
Cadena = "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ", longitud = 36
Primer duplicado en las posiciones 10 y 25, caracter = '0', valor hex = 30
</pre>
 
 
=={{header|Go}}==
Line 1,956 ⟶ 2,167:
XYZ ZYX 7 no 'Z' 5A 3 5
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no '0' 30 10 25
</pre>
 
===Using Java 11===
<syntaxhighlight lang="java">
 
import java.util.HashSet;
import java.util.List;
import java.util.OptionalInt;
import java.util.Set;
 
public final class DetermineUniqueCharacters {
 
public static void main(String[] aArgs) {
List<String> words = List.of( "", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" );
for ( String word : words ) {
Set<Integer> seen = new HashSet<Integer>();
OptionalInt first = word.chars().filter( ch -> ! seen.add(ch) ).findFirst();
if ( first.isPresent() ) {
final char ch = (char) first.getAsInt();
final String hex = Integer.toHexString(ch).toUpperCase();
System.out.println("Word: \"" + word + "\" contains a repeated character.");
System.out.println("Character '" + ch + "' (hex " + hex + ") occurs at positions "
+ word.indexOf(ch) + " and " + word.indexOf(ch, word.indexOf(ch) + 1));
} else {
System.out.println("Word: \"" + word + "\" has all unique characters.");
}
System.out.println();
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Word: "" has all unique characters.
 
Word: "." has all unique characters.
 
Word: "abcABC" has all unique characters.
 
Word: "XYZ ZYX" contains a repeated character.
Character 'Z' (hex 5A) occurs at positions 2 and 4
 
Word: "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" contains a repeated character.
Character '0' (hex 30) occurs at positions 9 and 24
</pre>
 
Line 3,386 ⟶ 3,643:
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36): '0' (0x30) duplicates at 9, 24
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ over find swap found ] is has ( $ c --> b )
 
[ dip [ 0 0 true ]
dup size 2 < iff
drop done
dup size 1 - times
[ behead 2dup has iff
[ swap find
dip not
2swap 2drop
i^ tuck + 1+ rot
0 conclude ] done
drop ]
drop ] is uniquechars ( $ --> n n b )
 
[ dup say 'String "'
echo$
say '" has length '
dup size echo
say ". "
dup uniquechars iff
[ say "There are no duplicated characters."
drop 2drop ]
else
[ rot over peek
dup say 'The character "'
emit
say '" (hex:'
16 base put
echo
base release
say ") is at positions "
swap echo
say " and "
echo
say "." ]
cr ] is task ( $ --> )
 
$ "" task
$ "." task
$ "abcABC" task
$ "XYZ ZYX" task
$ "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" task</syntaxhighlight>
 
{{out}}
 
<pre>String "" has length 0. There are no duplicated characters.
String "." has length 1. There are no duplicated characters.
String "abcABC" has length 6. There are no duplicated characters.
String "XYZ ZYX" has length 7. The character "X" (hex:58) is at positions 0 and 6.
String "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" has length 36. The character "0" (hex:30) is at positions 9 and 24.
</pre>
 
=={{header|R}}==
Most of this is adapted from [[Determine if a string has all the same characters#R]].
Line 3,522 ⟶ 3,835:
Raku works with unicode natively and handles combining characters and multi-byte emoji correctly. In the last string, notice the the length is correctly shown as 11 characters and that the delta with a combining circumflex in position 6 is not the same as the deltas without in positions 5 & 9.
 
<syntaxhighlight lang="raku" line=""> -> $str {
my $i = 0;
print "\n{$str.raku} (length: {$str.chars}), has ";
my %m = $str.comb.Bag;
%m{$_}.push: ++$i for $str.comb;
if any(%m.values) > 1 {
say "duplicated characters:";
Line 3,655 ⟶ 3,967:
Input = '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ', length = 36
First duplicate at positions 10 and 25, character = '0'
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → string
≪ "All chars unique" ""
1 string SIZE '''FOR''' j
string j DUP SUB
'''IF''' DUP2 POS
'''THEN''' DUP " duplicated at " +
string ROT POS →STR + " and " + j →STR +
ROT DROP SWAP
string SIZE 'j' STO
'''ELSE''' + '''END NEXT'''
DROP
≫ ≫ ''''UNICH?'''' STO
|
'''UNICH?''' ''( "string" -- "report" )''
initialize stack
scan string
extract jth character
if already seen
generate report
.
.
exit loop
else add the char to already seen list
clean stack
.
|}
{{in}}
<pre>
≪ { "" "." "abcABC" "XYZ ZYX" "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" } → cases
≪ 1 cases SIZE FOR n cases n GET UNICH? NEXT
≫ ≫ ´TASK’ STO
</pre>
{{out}}
<pre>
5: "All chars unique"
4: "All chars unique"
3: "All chars unique"
2: "Z duplicated at 3 and 5"
1: "0 duplicated at 10 and 25"
</pre>
 
Line 3,870 ⟶ 4,230:
--> Character 'é' (hex: 0xe9) reappears at indexes: 1 3.
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
Dim input() = {"", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"}
For Each s In input
Console.WriteLine($"'{s}' (Length {s.Length}) " + String.Join(", ", s.Select(Function(c, i) (c, i)).GroupBy(Function(t) t.c).Where(Function(g) g.Count() > 1).Select(Function(g) $"'{g.Key}' (0X{AscW(g.Key):X})[{String.Join(", ", g.Select(Function(t) t.i))}]").DefaultIfEmpty("All characters are unique.")))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>'' (Length 0) All characters are unique.
'.' (Length 1) All characters are unique.
'abcABC' (Length 6) All characters are unique.
'XYZ ZYX' (Length 7) 'X' (0X58)[0, 6], 'Y' (0X59)[1, 5], 'Z' (0X5A)[2, 4]
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' (Length 36) '0' (0X30)[9, 24]</pre>
 
=={{header|V (Vlang)}}==
Line 3,970 ⟶ 4,311:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var analyze = Fn.new { |s|
Line 4,098 ⟶ 4,439:
^ ^ Duplicate character: u, hex 75
</pre>
 
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">sub caracteresunicos (cad$)
local lngt
lngt = len(cad$)
print "cadena = \"", cad$, "\", longitud = ", lngt
for i = 1 to lngt
for j = i + 1 to lngt
if mid$(cad$,i,1) = mid$(cad$,j,1) then
print " Primer duplicado en las posiciones ", i, " y ", j, ", caracter = \'", mid$(cad$,i,1), "\', valor hex = ", hex$(asc(mid$(cad$,i,1)))
print
return
end if
next j
next i
print " Todos los caracteres son unicos.\n"
end sub
 
caracteresunicos ("")
caracteresunicos (".")
caracteresunicos ("abcABC")
caracteresunicos ("XYZ ZYX")
caracteresunicos ("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")</syntaxhighlight>
{{out}}
<pre>
cadena = "", longitud = 0
Todos los caracteres son unicos.
 
cadena = ".", longitud = 1
Todos los caracteres son unicos.
 
cadena = "abcABC", longitud = 6
Todos los caracteres son unicos.
 
cadena = "XYZ ZYX", longitud = 7
Primer duplicado en las posiciones 1 y 7, caracter = 'X', valor hex = 58
 
cadena = "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ", longitud = 36
Primer duplicado en las posiciones 10 y 25, caracter = '0', valor hex = 30
</pre>
 
 
=={{header|zkl}}==
9,479

edits