How to remove duplicates from TextBox?

I have a text box in which there is every element of a new line. I am trying to remove duplicates from this text box. I can’t think of anything. I tried to add each element to the array and remove duplicates, but it does not work. Are there any other options?

+3
source share
3 answers
yourTextBox.Text = string.Join(Environment.NewLine, yourArray.Distinct());
+8
source

Based on what Anthony Pegham wrote, but without a separate array:

yourTextBox.Text = string.Join(Environment.NewLine, yourTextBox.Lines.Distinct());

+3
source

public static string[] RemoveDuplicates(string[] s)
{
    HashSet<string> set = new HashSet<string>(s);
    string[] result = new string[set.Count];
    set.CopyTo(result);
    return result;
}

.

+1

Source: https://habr.com/ru/post/1782974/


All Articles