Remove Items from list....Brain teaser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Remove Items from list....Brain teaser

    Hi guys

    I've got the following list

    Code:
    List<string> modelList = new List<string>() { "A", "B", "B", "A", "B", "A", "B" };
    How will you make the list to only show the B's.

    Rules.
    No Linq
    No new list variable or no use of array

    So basically take the list as is, remove all A's

    (Just interested in finding out how you guyz will do it)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I would use the RemoveAll method.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'm not sure about the rules.

      If you want to remove all "A"s and "B"s then just use modelList.Clear.

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        No LINQ? Why would you take that away?

        Comment

        • akshaymahajan
          New Member
          • Sep 2009
          • 12

          #5
          Code:
          private void RemoveA()
                  {
                      List<string> modelList = new List<string>() { "A", "B", "B", "A", "B", "A", "B" };
                      for (int i = 0; i < modelList.Count; i++)
                      {
                          if (modelList[i] == "A")
                              modelList.RemoveAt(i);
                      }
                      for (int i = 0; i < modelList.Count; i++)
                          Console.WriteLine(modelList[i]);
                  }
          Last edited by Rabbit; Oct 16 '12, 04:26 PM. Reason: Please use code tags when posting code.

          Comment

          Working...