TASK: Concatenate and sort two strings. The letters must be distinct in output
C#:
using System;
using System.Linq;
using static System.Console;
namespace demo
{
class Program
{
static void Main(string[] args)
{
WriteLine(Longest("aretheyhere", "yestheyarehere")); // "aehrsty"
}
public static string Longest(string s1, string s2)
{
string str = s1 + s2;
char[] uniq_str = str.ToCharArray().Distinct().ToArray();
Array.Sort(uniq_str);
string result = String.Join("", uniq_str);
return result;
}
}
}
Result:
aehrsty
Better code from a better coder:
public static string Longest(string s1, string s2)
{
return new string((s1 + s2).Distinct()
.OrderBy(x => x).ToArray());
}
Reference: Two to One ( Codewars )