Deleting all same values from Generic List

user3256204

I have one generic list which contains some string value. Sometimes the values will be repeat. And I have to delete the specific values from the List Collection. I tried the following code.

List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B","A2B" };
     string currentPostalCode = "A2B";
     postalCodes.RemoveAt(postalCodes.IndexOf(currentPostalCode));

But this code removing the item from the position 1, but not from 3. How can I delete from the both positions? Please hep me. Thanks in advance.

MarcinJuraszek

Use List<T>.RemoveAll Method:

postalCodes.RemoveAll(c => c == currentPostalCode);

If you want to use RemoveAt you have to do it in a loop:

int index;
while((index = postalCodes.IndexOf(currentPostalCode)) != -1)
{
    postalCodes.RemoveAt(index);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

deleting empty values from list inside list

From Dev

Deleting negated values from a list in Scheme

From Dev

Deleting elements from dictionary values (lists of list)

From Dev

Find all rows with with values from list and which all have the same id MYSQL

From Dev

SQL Query: Deleting rows from PostgreSQL with same values

From Dev

Deleting all rows from table that share the same ID

From Dev

Creating List<Integer> with all same values in JAVA

From Dev

Macro for deleting values from certain columns in a row based on a list?

From Dev

removing a value from a linked list/deleting values that were not dynamically allocated

From Dev

Find and convert to NULL all duplicated values from the same column in a list of dataframes

From Dev

Deleting from a Linked List

From Dev

Deleting from a List in C

From Dev

Deleting characters from a list

From Dev

All the combinations of values from the same column

From Dev

How to get all values from same column?

From Dev

Need some help in deleting the data from list and again append it in same list

From Dev

Select All object values to list from dictionary

From Dev

DataGridView write all values from a column to list

From Dev

python: getting all pairs of values from list

From Dev

How to get all values from a list?

From Dev

Convert all dictionary values from array to list

From Dev

A Generic way to create a checkable context menu from a list of enum values

From Dev

Deleting all items from listview

From Dev

How do i add all values with the same year in list?

From Dev

How to adjust all values in a list at the same time (in dart)?

From Dev

PHP - Deleting duplicate values with same ref

From Dev

PYTHON: Create sublists of same values from nested list into one list

From Dev

PYTHON: Create sublists of same values from nested list into one list

From Dev

Issue by deleting an item from a list

Related Related

  1. 1

    deleting empty values from list inside list

  2. 2

    Deleting negated values from a list in Scheme

  3. 3

    Deleting elements from dictionary values (lists of list)

  4. 4

    Find all rows with with values from list and which all have the same id MYSQL

  5. 5

    SQL Query: Deleting rows from PostgreSQL with same values

  6. 6

    Deleting all rows from table that share the same ID

  7. 7

    Creating List<Integer> with all same values in JAVA

  8. 8

    Macro for deleting values from certain columns in a row based on a list?

  9. 9

    removing a value from a linked list/deleting values that were not dynamically allocated

  10. 10

    Find and convert to NULL all duplicated values from the same column in a list of dataframes

  11. 11

    Deleting from a Linked List

  12. 12

    Deleting from a List in C

  13. 13

    Deleting characters from a list

  14. 14

    All the combinations of values from the same column

  15. 15

    How to get all values from same column?

  16. 16

    Need some help in deleting the data from list and again append it in same list

  17. 17

    Select All object values to list from dictionary

  18. 18

    DataGridView write all values from a column to list

  19. 19

    python: getting all pairs of values from list

  20. 20

    How to get all values from a list?

  21. 21

    Convert all dictionary values from array to list

  22. 22

    A Generic way to create a checkable context menu from a list of enum values

  23. 23

    Deleting all items from listview

  24. 24

    How do i add all values with the same year in list?

  25. 25

    How to adjust all values in a list at the same time (in dart)?

  26. 26

    PHP - Deleting duplicate values with same ref

  27. 27

    PYTHON: Create sublists of same values from nested list into one list

  28. 28

    PYTHON: Create sublists of same values from nested list into one list

  29. 29

    Issue by deleting an item from a list

HotTag

Archive