473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Remove Items from list....Brain teaser

Sl1ver
196 New Member
Hi guys

I've got the following list

Expand|Select|Wrap|Line Numbers
  1. List<string> modelList = new List<string>() { "A", "B", "B", "A", "B", "A", "B" };
  2.  
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)
Aug 20 '12 #1
4 1788
Rabbit
12,516 Recognized Expert Moderator MVP
I would use the RemoveAll method.
Aug 20 '12 #2
Frinavale
9,735 Recognized Expert Moderator Expert
I'm not sure about the rules.

If you want to remove all "A"s and "B"s then just use modelList.Clear.
Aug 20 '12 #3
PsychoCoder
465 Recognized Expert Moderator Contributor
No LINQ? Why would you take that away?
Aug 21 '12 #4
akshaymahajan
12 New Member
Expand|Select|Wrap|Line Numbers
  1. private void RemoveA()
  2.         {
  3.             List<string> modelList = new List<string>() { "A", "B", "B", "A", "B", "A", "B" };
  4.             for (int i = 0; i < modelList.Count; i++)
  5.             {
  6.                 if (modelList[i] == "A")
  7.                     modelList.RemoveAt(i);
  8.             }
  9.             for (int i = 0; i < modelList.Count; i++)
  10.                 Console.WriteLine(modelList[i]);
  11.         }
Oct 16 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: brendan | last post by:
here's a brain teaser for someone with more skills or at least more lateral thinking capability than me - done my nut over this one... have written a list manager in PHP which a) posts out new...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
0
by: Shawn K | last post by:
I populate a listbox with the following code when my form loads: lstBox.Items.Add("value"); Once the page loads, I give the user the ability to remove items from the list box by selecting an...
15
by: Chung Leong | last post by:
Here's a little brain teaser distilled from a bug that took me a rather long time to figure out. The two functions in the example below behave differently. The difference is easy to spot, of...
1
by: silverburgh.meryl | last post by:
How can I remove items from a stl vector if it exists on another vector? In Java, I can do this: vector v1 = new Vector(); // populate v1 vector v2 = new Vector(); // populate v2
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
2
RedSon
by: RedSon | last post by:
Given a directed graph of k-nodes such that the last node creates a cycle with any other node determine which node the last node's edge points to using the minimum amount of resources and without...
2
by: =?Utf-8?B?RHJEQkY=?= | last post by:
I understand that the Value put into a DataGridViewComboBoxCell has to be a member of the Items list or an exception is thrown. I also understand that you can override that exception by handling...
11
by: calred | last post by:
i am trying to remove items in a list if the items do not exist in another list but my program does not work the way i have expected for example: l = l2 = # l2 could be a list of raw...
0
by: MeMeSo | last post by:
Hi I appreciate your help in advance. I'm new to VBA - and would like to know how to add and remove items from a listbox (not the main table) via a subform instantly. 1. Code to populate listbox...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.