-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayDeletion.rb
More file actions
40 lines (32 loc) · 816 Bytes
/
arrayDeletion.rb
File metadata and controls
40 lines (32 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
arr = [5, 6, 5, 4, 3, 1, 2, 5, 4, 3, 3, 3]
# Delete an element from the end of the array
arr.pop
# => 3
# Delete an element from the beginning of the array
arr.shift
# => 5
# Delete an element at a given position
arr.delete_at(2)
# => 4
# Delete all occurrences of a given element
arr.delete(5)
# => 5
# arr
# => [6, 3, 1, 2, 4, 3, 3]
#solution
def end_arr_delete(arr)
# delete the element from the end of the array and return the deleted element
arr.pop
end
def start_arr_delete(arr)
# delete the element at the beginning of the array and return the deleted element
arr.shift
end
def delete_at_arr(arr, index)
# delete the element at the position #index
arr.delete_at(index)
end
def delete_all(arr, val)
# delete all the elements of the array where element = val
arr.delete(val)
end