How to delete an element from an array in Julia

We can perform multiple operations on the Julia array, such as insert, update, read, and delete. In this Answer, we will discuss the deletion operation of Julia arrays. This operation allows us to remove unwanted or irrelevant elements from arrays, retaining data integrity and achieving efficient memory management in data manipulation tasks.

Deletion use cases

The deletion of an element can be tricky because there are multiple deletion use cases, such as deleting the element from the beginning, end, or as illustrated in the image below.

Multiple use cases of the delete operation
Multiple use cases of the delete operation

In Julia, we use different functions to achieve the aforementioned cases of the delete operation on the array. Let’s see the functions that we can use for them:

# Delete the element at the start of array
popfirst!(array)
# Delete the element at the end of array
pop!(array)
# Delete the element at any index of array
splice!(array, index)
# Delete the element by name in an array
deleteat!(array, findall(x->x=="value",array))
# Keep some element and delete other elements
keepat!(array, indexes)
# Delete all elements from an array
empty!(array)
Functions for the delete operation in Julia

The specific deletion method depends on the desired outcome, making it essential to choose the right method for each scenario.

Code example

Let’s use all the functions to remove the element(s) from a given array and see how these functions work in the widget below.

stringArray = ["Ifrah", "Maham", "Fatima", "Maham"]
# Delete the element at the start of array
popfirst!(stringArray)
println("Start element =", stringArray)
stringArray = ["Ifrah", "Maham", "Fatima", "Maham"]
# Delete the element at the end of array
pop!(stringArray)
println("Last elemen t=", stringArray)
stringArray = ["Ifrah", "Maham", "Fatima", "Maham"]
# Delete the element at any index of array
splice!(stringArray, 3)
# Alternate we can use deleteat!(stringArray, 3)
println("Element at specific index =", stringArray)
stringArray = ["Ifrah", "Maham", "Fatima", "Fabiha"]
# Delete the element by name in an array
deleteat!(stringArray, findall(i->i=="Maham",stringArray))
println("Element by name =", stringArray)
stringArray = ["Ifrah", "Maham", "Fatima", "Fabiha"]
# Delete the element by name in an array
keepat!(stringArray, 2:3) # Remove elements except elements at index 2 to 3
println("Keep some elements =", stringArray)
stringArray = ["Ifrah", "Maham", "Fatima", "Fabiha"]
# Delete all elements from an array
empty!(stringArray)
println("Delete all elements =", stringArray)

Note: In Julia arrays, the index starts at 1, instead of 0.

In conclusion, Julia provides multiple built-in deletion methods for different use cases. The appropriate method for removing an array’s element(s) depends on the specific use case. You can use any method according to your specific requirements.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved