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

Content added Content deleted
(→‎Raku: use Bag)
(Grouping BASIC dialects)
Line 775: Line 775:
</pre>
</pre>


=={{header|BQN}}==
=={{header|BASIC}}==
==={{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|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="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|BQN}}==
O(n^2) method used for finding indices.
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]]
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←=⌜˜
<syntaxhighlight lang="bqn">Check←=⌜˜
Line 1,475: Line 1,623:
LEN: 36. Duplicate chars. First duplicate at positions 10 and 25 where a "0"(hex:30) was found.
LEN: 36. Duplicate chars. First duplicate at positions 10 and 25 where a "0"(hex:30) was found.


</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>
</pre>


Line 4,108: Line 4,165:
--> Character 'é' (hex: 0xe9) reappears at indexes: 1 3.
--> Character 'é' (hex: 0xe9) reappears at indexes: 1 3.
</pre>
</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)}}==
=={{header|V (Vlang)}}==
Line 4,336: Line 4,374:
^ ^ Duplicate character: u, hex 75
^ ^ Duplicate character: u, hex 75
</pre>
</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}}==
=={{header|zkl}}==