0

My code believe needs to remove unique instancs of the same word (Complete word).

What do I mean by complete words. Well, given the following string:

THIIS IS IS A TEST STRRING

I need this returned:

THIIS IS A TEST STRRING

My code returns this:

THIS IS A TEST STRING

            var items = sString.Split(' ');
            var uniqueItems = new HashSet<string>(items);
            foreach (var item in uniqueItems)
            {
                strBuilder.Append(item);
                strBuilder.Append(" ");
            }

           finalString =  strBuilder.ToString().TrimEnd();

How can i therefore, retain an instance of a duplicate characters within a word, but remove complete duplicate words entirley?

CSharpNewBee
  • 1,951
  • 6
  • 28
  • 64

2 Answers2

3

You need Split and Distinct

var words = "THIIS IS IS A TEST STRRING".Split(' ').Distinct();

var result = string.Join(" ", words);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • "The Distinct method returns an **unordered** sequence that contains no duplicate values" http://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx – Joe Mar 18 '14 at 11:53
  • 2
    @Joe does it change the elements order ? **no.** – Selman Genç Mar 18 '14 at 11:54
  • What i don't like in this simple and readable approach is that it removes line-breaks and tabs(due to `Split()` instead of `Split(' ')`) and that it removes multiple consecutive spaces (due to `string.Join`). – Tim Schmelter Mar 18 '14 at 11:57
  • @TimSchmelter that's a good point.Thanks for suggestion.I updated my answer – Selman Genç Mar 18 '14 at 11:58
  • @Selman22: This has another issue. Now you keep line-breaks but it won't split on a new line when there is no space between. My second point still holds true(because `Distinct` removes multiple spaces) i don't want to nitpick, +1 anyway – Tim Schmelter Mar 18 '14 at 11:59
  • There's another problem if OP wants to remove only duplicate **consecutive** words like `IS IS` and not all like in this string: `THIS STRING IS IS A TEST STRING` – Tim Schmelter Mar 18 '14 at 12:15
  • Gents, this is a single string, not a sentence. I loop through the spliting into new lines etc prior to removing all duplicats. So, foreach string in list etc. – CSharpNewBee Mar 18 '14 at 13:00
  • @Selman22 Yes, it appears to do the right thing on your computer. I was just pointing out that it looks like the interface doesn't guarantee what the question appears to require. – Joe Mar 18 '14 at 14:29
  • 1
    @Joe: for LINQ to Objects it is guaranteed. It won't change the order. That said, MS doesn't want to give a binding guarantee. http://stackoverflow.com/questions/4734852/does-c-sharp-distinct-method-keep-original-ordering-of-sequence-intact – Tim Schmelter Mar 18 '14 at 15:57
  • Good to know, thanks. I think it's better to have had that conversation and for you to have posted that link than not. – Joe Mar 18 '14 at 16:01
0

bro Call this method i know its a bit complex and lengthy but you will be amazed after getting the results!

public string IdentifySamewords(string str)
{
   string[] subs=null;
   char[] ch=str.ToCharArray();
   int count=0;
   for(int i=0;i<ch.Length;i++)
   {
     if(ch[i]==' ')
       count++;
   }
   count++;
   subs=new string[count];
   count=0;
   for(int i=0;i<ch.Length;i++)
   {
      if(ch[i]==' ')
       count++;
      else
       subs[count]+=ch[i].ToString();
   }
   string current=null,prev=null,res=null;
   for(int i=0;i<subs.Length;i++)
   {
      current=subs[i];
      if(current!=prev)
        res+=current+" ";

      prev=current;
   }
  return res;
}
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29
  • 1
    Nope, i pass in a string that contains the word Brothers, twice and it returns the same string i input – CSharpNewBee Mar 18 '14 at 12:58
  • i wonder how you are using it i passed the same input in which you are getting the same result but i am not it is returning only Brothers when i pass it two times! :O – Zain Ul Abidin Mar 19 '14 at 05:40