# Newbie delete elements from array question

**URL:** <https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481>\
**Category:** New to Julia\
**Created:** [February 20, 2024, 10:03pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481 "2024-02-20T22:03:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 10:03pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/1 "2024-02-20T22:03:01Z")

</div>

I have an 1-D array `A` and an array of integers `index` that contains some of the indices of array `A`. I want to compute a new array from `A` with the elements with indices in `index` removed. Is there a slick Julia way of doing this. The way I would do this is by concatenating the subarrays of `A` that do not contain the indices in `index`. If it helps the `index` array is sorted. What I am actually trying to do is remove from the array all consecutive pairs of elements that are equal starting at the beginning so that `[1,1,1,2]` would reduce to `[1,2]`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 20, 2024, 10:06pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/2 "2024-02-20T22:06:21Z")

</div>

`deleteat!` should do what you want. (e.g. `deleteat!([1,1,1,2], [2,3])==[1,2]`)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 20, 2024, 10:17pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/3 "2024-02-20T22:17:33Z")

</div>

maybe you want `unique`:

```julia
julia> show(unique([1,1,1,2]))
[1, 2]

```

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 10:30pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/4 "2024-02-20T22:30:11Z")

</div>

Thank you! Thank you! Thank you!

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 20, 2024, 11:11pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/5 "2024-02-20T23:11:35Z")

</div>

Okay, looking at the original post, `unique` may not be the goal, rather something like:

```julia
squish(v) = foldl((r,e)->(last(r)==e || push!(r,e); r), v; init=[v[1]])

squish([1,1,1,2]) # == [1,2]

```

Using this `squish` would be faster than calling `deleteat!` in almost all cases.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 11:24pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/6 "2024-02-20T23:24:44Z")

</div>

My array are almost always less than 16 in length. Would this then still be faster than deleteat!?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 20, 2024, 11:33pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/7 "2024-02-20T23:33:38Z")

</div>

Okay, 16 may still be enough for `squish` above to be faster. But to get real speed, you have to be a little more detailed:

```julia
function squish!(v)
    isempty(v) && return v
    i, e = 2, v[1]
    @inbounds for j in 2:length(v)
        new_e = v[j]
        new_e == e && continue
        v[i] = new_e
        i += 1
        e = new_e
    end
    return resize!(v, i-1)
end

```

This gets top speed, but mutates the input vector:

```julia
julia> v = [1,1,1,2];

julia> squish!(v)
2-element Vector{Int64}:
 1
 2

julia> @btime squish!(v) setup=(v = [1,1,1,2])
  4.131 ns (0 allocations: 0 bytes)
2-element Vector{Int64}:
 1
 2

```

To keep the input vector, use `copy` or the previous `squish`:

```julia
julia> squish!(copy(v))
2-element Vector{Int64}:
 1
 2

```

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 21, 2024, 12:12am UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/8 "2024-02-21T00:12:06Z")

</div>

Thank you. I will be experimenting with everyone’s suggestions. For what it is worth I am calculating the geometric product of two bases in a geometric algebra. This requires reducing the integer list to normal order using certain rules. Each entry in the list refers to a basis vector in the algebra. For example [1,3,2] =\> ex_ez_ey and [1] =\> ex, etc… The basis vectors are not necessarily orthogonal.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 21, 2024, 12:52am UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/9 "2024-02-21T00:52:24Z")

</div>

Ah… maybe something like this would interest you then:

```julia
freeprod(v) = foldl(v; init=Tuple{eltype(v),Int}[]) do r,e
    if isempty(r) || first(r[end])!=e
        push!(r, (e,1))
    else
        r[end] = (r[end][1], r[end][2]+1)
    end
    r
end

```

which gives:

```julia
julia> freeprod(v)
2-element Vector{Tuple{Int64, Int64}}:
 (1, 3)
 (2, 1)

```

a list of values and ‘exponents’.

Or, as an educational example, another way to code `freeprod` (which is slightly slower):

```julia
using IterTools

freeprod2(v) = Iterators.map(g->(first(g),length(g)),
  IterTools.groupby(identity, v)) |> collect

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [February 21, 2024, 4:00am UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/10 "2024-02-21T04:00:36Z")

</div>

The way I understand the problem, `unique!` is the solution. Wait with complicated speed optimisations until you know that it will make a noticeable difference.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 21, 2024, 3:29pm UTC](https://discourse.julialang.org/t/newbie-delete-elements-from-array-question/110481/11 "2024-02-21T15:29:20Z")

</div>

Since you are being so helpful let me give you and example of what I am doing. For 3 dimensions the basis vectors are represents by `[1]=>e_1, [2]=>e_2, and [3]=>e_3` and the dot products of the basis vectors (metric tensor) are `g_ij = e_i⋅e_j`. Then the basis of the geometric algebra is `[1], [2], [3], [1,2], [2,3], [1,3], [1,2,3]` (`[1,2,3]=>e_1e_2e_3`)the geometric product of three vectors which is associative ) and an element of the algebra is linear combination of the basis vectors and a scalar and is called a multivector. To multiply multivectors we need to multiply bases and get an answer as a multivector. The reduction formula where `a` and `b` are vectors is `ba = 2a⋅b-ab` (Note that `aa = 2a⋅a-aa` or `aa = a⋅a` ). Then for example -

```julia

[1,2,3][1,2] = [1,2,3,1,2] = [1,2](2g_31-[1,3])[2] = 2g_31[1,2,2]-[1,2,1,3,2] = 2g_31g_22[1]-([1,2,1](2g_32-[2,3])) = 2g_31g_22[1]-2g_32[1,2,1]+[1,2,1,2,3] =

2g_31g_22[1]-2g_32(2g_21[1]-[1,1,2])+[1,2,1,2,3] = 2g_31g_22[1]-2g_32*2g_21[1]+4g_31g_22g_11[2]+[1,2,1,2,3]=

2g_31g_22[1]-2g_32*2g_21[1]+4g_31g_22g_11[2]+([1,2](2g_21-[2,1])[3]) = 2g_31g_22[1]-2g_32*2g_21[1]+4g_31g_22g_11[2]+2g_21[1,2,3]-[1,1,2,2,3] =

(2g_31g_22-2g_32*2g_21)[1]+4g_31g_22g_11[2]-g_11g_22[3]+2g_21[1,2,3]

```

or the simple examples

```julia

[2,1] = 2g_21 - [1,2]

[1,1] = g_11

[1,2,2] = g_22[1]

```
