# Vector and integer variables

**URL:** <https://discourse.julialang.org/t/vector-and-integer-variables/41349>\
**Category:** New to Julia\
**Tags:** vector\
**Created:** [June 14, 2020, 1:43am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349 "2020-06-14T01:43:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marques](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marques/32/13630_2.png) [@Marques](https://discourse.julialang.org/u/Marques)\
**Post date:** [June 14, 2020, 1:43am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/1 "2020-06-14T01:43:28Z")

</div>

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 (:

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 14, 2020, 1:51am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/2 "2020-06-14T01:51:44Z")

</div>

I am probably misinterpreting something (see below)

```julia
x = 123
v = [1,2,3,123]
x in v # returns true
v = [1, 2, 3]
x in v # returns false

```

```julia
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 `x`s 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?

---

<div class="post-metadata">

**Author:** ![Marques](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marques/32/13630_2.png) [@Marques](https://discourse.julialang.org/u/Marques)\
**Post date:** [June 14, 2020, 1:59am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/3 "2020-06-14T01:59:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 14, 2020, 2:27am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/4 "2020-06-14T02:27:57Z")

</div>

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

```julia
x = 123
digitsofx = digits(x) # returns [3,2,1]
x = 1232
digitsofx = unique(digits(x)) # returns [2,3,1]

```

```julia
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_

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 14, 2020, 11:05am UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/5 "2020-06-14T11:05:45Z")

</div>

> [@JeffreySarnoff](#):
>
> `all(map(x->in(x,v), digitsofx))`

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

```julia
all(in(v), digitsofx) 

```

?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 14, 2020, 12:20pm UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/6 "2020-06-14T12:20:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marques](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marques/32/13630_2.png) [@Marques](https://discourse.julialang.org/u/Marques)\
**Post date:** [June 14, 2020, 2:49pm UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/7 "2020-06-14T14:49:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 14, 2020, 6:07pm UTC](https://discourse.julialang.org/t/vector-and-integer-variables/41349/8 "2020-06-14T18:07:49Z")

</div>

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