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
2010-03-31 15:31:31
it wont ..what will happen if the values are 1 2 3 4 2 6 7 ??? the above will work?
2010-03-31 15:36:07
yep, my code assumes strings is an array of strings, but you can apply the same logic to an array of integers too.
HollyStyles
2010-03-31 15:39:17
i mean if the 2 is repeating not just as the next number , it is repeating after 4 strings .. hence the above wont work
2010-03-31 15:48:43
Aha! ok now I see what you mean.
HollyStyles
2010-03-31 15:54:45
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
2010-03-31 15:31:49
@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
2010-03-31 15:38:11