Click here to Skip to main content
15,920,503 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi dear friends,
I have some question,
Now I have a string collection but that collection has multiple same item, my wish , find same item and add counter after remove one of the.

C#
MatchCollection matches = Regex.Matches(text.ToLower(), pattern);
                    for (int i = 0; i < matches.Count; i++)
                    {
                        string s = "<li>" + matches[i] + "</li>";
                        string[] stringArray = s.Split(' ');
                        if (stringArray.Length>1)
                        {
                            for (int j = 0; j < matches.Count; j++)
                            {
                                if (matches[i].ToString() == matches[j].ToString() && i == j)
                                {
                                    counter++;
                                }
                            }
                            var newWord=new Vocabulary {Word = matches[i].ToString(),Frequency = counter};
                            counter = 0;
                            listSentences.Add(newWord);
                        }
                    }


This is my Code but not working according to me.
Where is the my mistake?

Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jul-13 12:55pm    
First of all, matches[i] is not a string, its type is Match. All matches will really be different. Are you sure you need to catch those with the same .ToString values?
And you are not really trying to do what you want.
—SA
ammoti 16-Jul-13 17:22pm    
Thank you for comment.
You right, i didnt correct explain. Firstly, in the text i find matches item with regular expression so my want for example, there are 2 lorem ipsum but i want only add my collection one lorem ipsum.
Sergey Alexandrovich Kryukov 16-Jul-13 23:25pm    
I understand. Do you need a collection of strings on output? If so, I explained it in my answer.
—SA

Please see my comment to the question.

And here is a little hint on how could you solve the problem: use another collection, the one of the type System.Collections.Generic.HashSet<element_type></element_type>, where the generic parameter ELEMENT_TYPE should be of the type Match or, more likely, string, depending on what you really want. The hash set don't allow duplicates. You put all data in the set without doubling. If you want, at the end you can use HashSet.ToList or HashSet.ToArray, to get a list or an array of items. The performance will be much better, and the logic is much simpler.

—SA
 
Share this answer
 
I think you are exactly expecting like below example.please run this sample and verify it
C#
class Program
   {
       static void Main(string[] args)
       {
           var s1 = new List<string> { "Item1", "Item2", "Item1", "Item2" };
           var finalResult = new List<FinalResult>();
           foreach (var item in s1.Distinct())
           {
               string item1 = item;
               var data = s1.Where(x => x == item1);
               if(data.Count()>1)
               {
                   finalResult.Add(new FinalResult{ItemName = item1,Count = data.Count()});
               }
           }

           Console.ReadLine();
       }
       public class FinalResult
       {
           public string ItemName { get; set; }
           public int Count { get; set; }
       }
   }

Hope this helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900