views:

43

answers:

2

i have a loop where a variable value will change in each loop and display those values in each loop. i need to skip the display of the value if the same value repeats from second time

+2  A: 

You need to remove the duplicates from the list. Ive chosen StringCollection from the System.Collections.Specialized namespace. But you could use List from System.Collections.Generic

String[] strings = new String[] { "1", "2", "3", "4", "2", "5", "4", "6", "7" };
StringCollection unique = new StringCollection();

foreach (String s in strings)
{
     if (!unique.Contains(s))
         unique.Add(s);
}

foreach (String s in unique)
{
     Console.WriteLine(s);
}
HollyStyles
it wont ..what will happen if the values are 1 2 3 4 2 6 7 ??? the above will work?
yep, my code assumes strings is an array of strings, but you can apply the same logic to an array of integers too.
HollyStyles
i mean if the 2 is repeating not just as the next number , it is repeating after 4 strings .. hence the above wont work
Aha! ok now I see what you mean.
HollyStyles
A: 

in speudo code

 declare var1

 for each item in the collection
      check if item is var1, if not print item
      set item to var1
Fredou
The question has a C# tag.
Seb
@seb, reading the question and looking at his history, it seem more a "can you do this for me" or a "i have to do this for a homework". I prefer giving hint, not doing it in these case.
Fredou
could u answer this instead of blaming?
it wont ..what will happen if the values are 1 2 3 4 2 6 7 ??? the above will work?