# Remove an element from an array without changing the original

**URL:** <https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103>\
**Category:** General Usage\
**Tags:** array\
**Created:** [June 17, 2021, 4:45pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103 "2021-06-17T16:45:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [June 17, 2021, 4:45pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/1 "2021-06-17T16:45:09Z")

</div>

I want to delete a element from an array without changing the original. Like this:

```julia
original_array = [12,13,14,15]
new_array = function_to_delete_a_element(element_to_delete = 14, array = original_array)
print(new_array)
>[12,13,15]
print(original_array)
>[12,13,14,15] #Here, all the functions that I have looked for to eliminate, change the original array, I want to keep it

```

Above is what I want to do, but all the functions I have found also remove the element from the original array, and when i print the original array, throw this:

```julia
print(original_array)
>[12,13,15] # I want to get this ---> [12,13,14,15]

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 17, 2021, 4:51pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/2 "2021-06-17T16:51:45Z")

</div>

Yes, it seems that it is as you describe. You can do:

```julia
julia> a=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b=deleteat!(deepcopy(a),2)
2-element Vector{Int64}:
 1
 3

julia> a
3-element Vector{Int64}:
 1
 2
 3

julia> b
2-element Vector{Int64}:
 1
 3

```

`julia> b=deleteat!(copy(a),2)`

Is enough in this case.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 17, 2021, 4:53pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/3 "2021-06-17T16:53:47Z")

</div>

Why not just:

```julia
@views deleteelem(i, a::AbstractVector) = vcat(a[begin:i-1], a[i+1:end])

```

to return a new array with index `i` removed.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [June 17, 2021, 4:55pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/4 "2021-06-17T16:55:53Z")

</div>

Ok, but how do I do it indicating the particular element and not the index? Like this:

```julia
a =[12,13,14]
b=deleteat!(deepcopy(a),13)

```

PD: How do you write the code for Julia here? 🙂

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 17, 2021, 5:04pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/5 "2021-06-17T17:04:52Z")

</div>

Maybe you want `filter`?

```julia
julia> filter(!=(14), [12,13,14,15])
3-element Vector{Int64}:
 12
 13
 15

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 17, 2021, 5:34pm UTC](https://discourse.julialang.org/t/remove-an-element-from-an-array-without-changing-the-original/63103/6 "2021-06-17T17:34:13Z")

</div>

> [@Sebastian\_Mena](#):
>
> Ok, but how do I do it indicating the particular element and not the index?

Call `findfirst(==(13), a)` to get the index of the first time it appears? Or use `filter` (which removes _all_ occurrences of a particular value).

Or use some other data structure (e.g. `Set`) that is addressed by value.
