Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a list does not contain an element?

Tags:

I am trying to make a small program in which checks to see if the box is checked and if it is it will add an element to the list "names". But I need it to check if the name isn't already in the list before it adds the element.

like image 916
user2534753 Avatar asked Jun 29 '13 14:06

user2534753


People also ask

How do you check if a list does not contain an element?

List<T>. Contains(T) Method is used to check whether an element is in the List<T> or not.

How do you check if a list does not contain an element in Java?

contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.

How do you check if a list does not contain a value in Python?

To check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not.

How to check if element is in C #list using contains ()?

Example 1 – Check if Element is in C# List using Contains () In the following program, we have a list of integers. We shall check if element 68 is present in the list or not using Contains () method. As 68 is present in the list, List.Contains () method returns True.

How to check if an element is present in the list?

To check if an element is present in the list, use List.Contains () method. The definition of List.Contains () method is given below. If given element is present in the list, then List.Contains () returns True, else, it returns False.

How to check if an element exists in list in Python?

We can use the in-built python List method, count (), to check if the passed element exists in List. If the passed element exists in the List, count () method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

How to check if a list element exists in R?

As you can see, the RStudio console returns FALSE. Another alternative for checking whether a list element exists is provided by the is.null function. As in Example 1, the following R syntax returns TRUE in case the element “numbers” is part of our example list: A third alternative is provided by the exists function of the R programming language.


1 Answers

The Contains method

if (!myList.Contains("name"))
{
    myList.Add("name");
}

Or Any method

if (!myList.Any(s => s == "name"))
{
    myList.Add("name");
}

would do the job. You don't specify whether the check is case sensitive or not, these checks are both case sensitive but it's easy enough to update for case insensitive checks.

like image 110
James Avatar answered Oct 27 '22 09:10

James