Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using Visual Studio 2008 I have a code that generates random characters depending on the current date. When time is not changing fast enough, or the program begins to malfunction, I have repeating numbers. each numeric string is placed into a textbox and each time the numeric string is produced, it is placed into a new line. I need a code snippet that will find these repeating string which contain a number like so:
162763-115235-205.2218

I need a piece of code that will show the repeated strings in another textbox for each repeated instance.

Thank You.

[edit]
Just a description of what my application does.
User presses button.
numeric text generates into the text box.
each line in the text box is a separate value.

now what I need to do is find each "value" that repeats and say so in another textbox.

thanks.
[/edit]
Posted
Updated 24-Feb-12 14:38pm
v5

1 solution

Just put all the Strings you produced into a List and then let loose the power of Enumerable.Distinct(Of TSource)-Methode (IEnumerable(Of TSource))[^].

[Edit]
While taking set differences may also have been an option I tried a different approach from above utilizing LinQ queries:

C#
public static void CountAllDoubles()
{
    // Create a list of String.
    List<String> listOfStrings = new List<String>{ "AAA", "BBB", "CCC", "AAA", "CCC", "AAA", "DDD" };

    var query = listOfStrings.GroupBy(
        element => element,
        (element, elements) => new
        {
            Key = element,
            Count = elements.Count(),
        });

    Console.WriteLine("Counting all occurrences including single ones:");
    foreach (var result in query)
    {
        Console.WriteLine("\tString: {0} Number of occurrences: {1}", result.Key, result.Count);
    }


    Console.WriteLine("\nCounting only multiple occurrences:");
    foreach (var result in query.Where(a => { return a.Count > 1;  }))
    {
        Console.WriteLine("\tString: {0} Number of occurrences: {1}", result.Key, result.Count);
    }
}



[/Edit]


Regards,

Manfred

As a side note, I just had an opportunity to use this today.
 
Share this answer
 
v3
Comments
vlad781 23-Feb-12 19:08pm    
The page you linked is in a language I do not understand. Please send one in either english or russian. Thank you. Or simply explain how I can use the method.
Orcun Iyigun 23-Feb-12 19:41pm    
There you go. This is the link[^] that in English.
vlad781 23-Feb-12 20:10pm    
thanks! Once i get this to work, I'll accept the answer.
vlad781 23-Feb-12 21:52pm    
This all makes sense, somewhat, but how do I use this method?
Manfred Rudolf Bihy 24-Feb-12 7:29am    
Please see my updated answer. I have added method using LinQ queries to solve you problem. :)

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