Search code examples
c#.netprogramming-languageslogicbusiness-logic

Logic to skip repeating values in a variable


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


Solution

  • 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);
    }