bbqwings
bbqwings

Reputation: 85

how to use linq to compare and replace duplicate string in one list

I want to check remove the duplicate in a string split by ;, but i don't know how to do next
my code like this:

    string str = "PORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR;/n/nabcdef1234;/n/nabcdef1234";
    List<string> unitList = new List<string>();

    foreach (string s in str.Replace("/n","").Split(';'))
    {
        unitList.Add(s);
    }

    if ((unitList.Distinct()).Count() == 1)
        Label1.Text=unitList.FirstOrDefault();
    else
    {
       //return more than 1 ,remove duplicate
       //replace duplicate by "-" or ""
    }

my output want to be:"PORTION G ON GROUND FLOOR;/n/n-;/n/nabcdef1234;/n/n-".

and I found if i use string[] array instead of List, the return (unitList.Distinct()).Count() is 2 instead of 1, i am wondering if anything wrong.
here is my code:

    string str = "PORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR";

    string[] unitList = new string[10];
    int i=0;
    foreach (string s in str.Replace("/n","").Split(';'))
    {

        unitList[i]=s;
            i++;
    }

I am pleased and thank anyone to help me to solve it!!

Upvotes: 0

Views: 556

Answers (3)

ocuenca
ocuenca

Reputation: 39326

This is a solution to your problem:

 string str = "PORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR;/n/nabcdef1234;/n/nabcdef1234";
 List<string> selected=new List<string>();
 var result = str.Split(';').Select(s =>
                                       {
                                         var word = s.Replace("/n", "");
                                         if (selected.Contains(word))
                                         {
                                           return s.Replace(word,"")+"-";
                                         }
                                         else
                                         {
                                           selected.Add(word);
                                           return s;
                                         }
                                        }).Aggregate<string, string>(null, (current, item) => current + item);

This code return the string that you are expecting.

But if you want the list of words without duplicates,this is another way to obtain it:

var list = str.Split(';').ToLookup(s => s.Replace("/n", "")).Select(e=>e.Key);

Upvotes: 1

Mahesh Malpani
Mahesh Malpani

Reputation: 1989

string str = "PORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR;/n/nabcdef1234;/n/nabcdef1234";
            var temp =  str.Replace("/n", string.Empty).Split(';').Distinct();
            var result = string.Join("/n", temp);

Hope its same thing you expecting

Upvotes: 0

AD.Net
AD.Net

Reputation: 13399

 string str = "PORTION G ON GROUND FLOOR;/n/nPORTION G ON GROUND FLOOR;/n/nabcdef1234;/n/nabcdef1234";

            var list = str.Replace("/n", "").Split(';');
            var result = (from s in list
                group s by s
                into g
                select g.First()).ToList();

You can just group by the string in the array itself after you split it and then select the first one.

Upvotes: 1

Related Questions