How do I remove duplicates from a C# array?

后端 未结 27 2211
北海茫月
北海茫月 2020-11-22 07:53

I have been working with a string[] array in C# that gets returned from a function call. I could possibly cast to a Generic collection, but I was w

27条回答
  •  既然无缘
    2020-11-22 08:28

    protected void Page_Load(object sender, EventArgs e)
    {
        string a = "a;b;c;d;e;v";
        string[] b = a.Split(';');
        string[] c = b.Distinct().ToArray();
    
        if (b.Length != c.Length)
        {
            for (int i = 0; i < b.Length; i++)
            {
                try
                {
                    if (b[i].ToString() != c[i].ToString())
                    {
                        Response.Write("Found duplicate " + b[i].ToString());
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Found duplicate " + b[i].ToString());
                    return;
                }
            }              
        }
        else
        {
            Response.Write("No duplicate ");
        }
    }
    

提交回复
热议问题