Jump to content

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

Added C#
(Added Sidef)
(Added C#)
Line 725:
Character '0' (hex 30) occurs at positions 10 and 25.
 
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Linq;
 
public class Program
{
static void Main
{
string[] input = {"", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"};
foreach (string s in input) {
Console.WriteLine($"\"{s}\" (Length {s.Length}) " +
string.Join(", ",
s.Select((c, i) => (c, i))
.GroupBy(t => t.c).Where(g => g.Count() > 1)
.Select(g => $"'{g.Key}' (0X{(int)g.Key:X})[{string.Join(", ", g.Select(t => t.i))}]")
.DefaultIfEmpty("All characters are unique.")
)
);
}
}
}</lang>
{{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>
 
196

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.