Vector and integer variables

Hi guys, could you help me please? I came across a problem and I can’t imagine a way out. How do I compare an integer value passed as a parameter to a function, for example: x = 123 and find out if the values of x exist in a given vector, for example, v = [9,4,2,1,3]. There is no way to pass this entire value as a vector, so how can I get each unit of the x value separately? I hope you haven’t been confused and thanks for your help (:

I am probably misinterpreting something (see below)

x = 123
v = [1,2,3,123]
x in v # returns true
v = [1, 2, 3]
x in v # returns false
x = [7, 123]
v = [1, 2, 3, 123]
result = map(in, x, v) # returns [false, true] (looks like [0,1])

fyi Just passing a vector to a function does not involve copying the vector.

For my own clarification, is your question:
With x as a single integer value and v as a vector of integers, how do I find out if there exists one or more xs in v?

or is it more like:
With x as a vector of one or more integer values and v as a vector of integers, how do I find out, for each of the integers in x, does it exist in v?

or something else?

1 Like

It is more the first situation. For a single value of x such as x = 123, find if the values like (1,2,3) that are part of x as a variable of the integer type exist in the vector v for example v = [1,2, 3]. In this case the vector v contains the units that make up the integer variable x.

You want to split the integer ‘x’ into its constituent digits and then check whether each digit appears in ‘v’?

x = 123
digitsofx = digits(x) # returns [3,2,1]
x = 1232
digitsofx = unique(digits(x)) # returns [2,3,1]
x = 1223
v = [1,3,5,7]
digitsofx = unique(digits(x)) # [3,2,1]
all_digits_in_v = all(in(v), digitsofx) # false
v = [1,3,5,2,7]
all_digits_in_v = all(in(v), digitsofx) # true

If x = 111 do you want to check if there is at least one ‘1’ in ‘v’ or do you want to check if there are at least three '1’s in ‘v’?

the nice form all(in(v), digitsofx) is thanks to @DNF


Do you need the map? Seems redundant and creates an unnecessary temporary array. What about

all(in(v), digitsofx) 



works for me :smile: good suggestion – I am editing the prior note, crediting you.

1 Like

Thank you so much for your help, I finally managed to understand :slight_smile:

Super! We are happy to have you with the Julia Community.