[デイリーコーディング]複数の文字列を連結/ソート/重複削除して返す


今日の問題: 複数の文字列を連結/ソート/重複削除して返せ

回答
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;
        }
    }
}
結果
aehrsty

ベターな回答:

回答
public static string Longest(string s1, string s2)
        {
            return new string((s1 + s2).Distinct().OrderBy(x => x).ToArray());
        }

参照: Two to One (Codewars)