A
A
Alexey Smirnov2016-03-11 22:11:01
Programming
Alexey Smirnov, 2016-03-11 22:11:01

How to remove duplicate words (and phrases) in a comma separated string?

There is some original line:

енот, енот, гваделупский енот, косумельский енот, енот, енот-полоскун, енот-ракоед, косумельский енот

I need to remove all duplicates of repeated words (and phrases) separated by commas from this line.
For example, in the above line, the word енотis repeated 3 times, and I need to remove 2 of these 3 words, leaving only the first word енот.
Also, the phrase косумельский енотis repeated 2 times, and you need to delete one of these phrases.
As a result, I would like to get the following final line:
енот, гваделупский енот, косумельский енот, енот-полоскун, енот-ракоед

How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-03-11
@ERAFY

ideone.com/4z7HsT

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
  public static void Main()
  {
    string text = "енот, енот, гваделупский енот, косумельский енот, енот, енот-полоскун, енот-ракоед, косумельский енот";
    var result = text.Split(',').Select(x => x.Trim()).Distinct().Aggregate((r, word) => r + ", " + word);
    Console.WriteLine(result);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question