To check if an element is in an array in Julia, you can use the in
keyword. Here's an example:
Check if an Element is in Array in Julia Examples
In the example below, the in
keyword is used to check if the element 3
is in the array a
. Since 3
is in the array, the expression 3 in a
returns true
.
julia> a = [1, 2, 3, 4, 5] julia> 3 in a true
Here's another example:
In this example, the in
keyword is used to check if the element 3
is in the array b
. Since 3
is not in the array, the expression 3 in b
returns false
.
julia> b = [6, 7, 8, 9, 10] julia> 3 in b false
Note that the in
keyword can only be used to check if an element is in an array. It cannot be used to check if an element is in other types of collections, such as sets or dictionaries. To check if an element is in a set or dictionary, you can use the in
keyword along with the keys
or values
function, depending on what you're looking for.
In the examples below, the in
keyword is used with the values
function to check if the element 3
is a key in the set s
, and with the values
function to check if the element 3
is a value in the dictionary d
. Since 3
is a key in s
and a value in d
, the expressions 3 in keys(s)
and 3 in values(d)
both return true
.
julia> s = Set([1, 2, 3, 4, 5]) julia> 3 in values(s) true julia> d = Dict(1 => "one", 2 => "two", 3 => "three") julia> 3 in values(d) true
Related:
- Check if a File Exists in Julia
- Check if a Variable Exists in Julia
- Check if a Key is in Dictionary using Julia