Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - REMOVE FROM ARRAY


How to remove an element from the Array by its name/value?


Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the Array.


It can be done with the delete() Method.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.delete("Kriti")
puts x


Output :



  Mohan
  Salim

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the delete() Method that searches for the name Kriti and removes it from the Array.


x.delete("Kriti")
java_Collections


And the new array looks like,


['Mohan', 'Salim']

How to remove an element from the Array by its index/position?


Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to remove the element at index/position 2 from the Array.


So, we can use del keyword or the pop() Method to remove an element from the Array.


Let us see the example with del keyword first.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.delete_at(2)
puts x 


Output :



  Mohan
  Kriti

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the del keyword that searches for the element at index/position 2 and removes it from the Array.


x.delete_at(2)

And as we can see, there is Salim at index/position 2. So Salim is removed from the Array.

java_Collections

And the new array is,


['Mohan', 'Kriti']

How to remove the first element from the Array?


The shift() Method can be used to remove an item from the end of the Array. If no value is specified at the parameter of shift() Method then it removes the first element by default.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.shift()
puts x


Output :



  Kriti
  Salim

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the shift() Method that searches for the first value by default(As parameter value is specified) and removes it from the Array.


x.shift()

And as we can see, there is Mohan at the front of the Array. So Mohan is removed from the Array.

java_Collections

And the new array is,


["Kriti", "Salim"]

How to remove any number of elements from the front of the Array?


Again the shift() Method can be used to remove any number of items from the end of the Array.


Just specify the number of elements to be removed from the end inside the parameter of shift() method.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.shift(2)
puts x 


Output :



  Salim

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the shift(2) Method with parameter 2 that searches for the first 2 values from the array (i.e. Mohan and Kriti) and removes from the array.


x.pop(2)

And as we can see, there is Mohan and Kriti at the front of the Array. So, Mohan and Kriti is removed from the Array.

java_Collections

And the new array is,


['Salim']

How to remove the last element from the Array?


The pop() Method can be used to remove an item from the end of the Array. If no value is specified at the parameter of pop(() Method then it removes the last element by default.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.pop()
puts x 


Output :



  Mohan
  Kriti

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the pop() Method that searches for the last value by default(As no index is specified) and removes it from the Array.


x.pop()

And as we can see, there is Salim at the end of the Array. So Salim is removed from the Array.

java_Collections

And the new array is,


['Mohan', 'Kriti']

How to remove any number of elements from the end of the Array?


Again the pop() Method can be used to remove any number of items from the end of the Array.


Just specify the number of elements to be removed from the end inside the parameter of pop(() method.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.pop(2)
puts x 


Output :



  Mohan

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the pop(2) Method with parameter 2 that searches for the last 2 values from the array (i.e. Kriti and Salim) and removes from the array.


x.pop(2)

And as we can see, there is Kriti and Salim at the end of the Array. So, Kriti and Salim is removed from the Array.

java_Collections

And the new array is,


['Mohan']

How to remove all the elements from the Array?


The clear() Method can be used to remove all the elements from the Array.


Example :



x = ["Mohan", "Kriti", "Salim"]
x.clear()
puts x


Output :



  []

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Next, we have used the clear() Method that removes all the elements from the Array making the Array empty.


And we get an empty Array as output,


[]

How to remove the duplicate elements from the Array?


The uniq() Method can be used to remove duplicate elements from the Array.


Example :



x = ["Mohan", "Kriti", "Salim", "Mohan", "Kriti", "Mohan"]
y = x.uniq()
puts y 


Output :



  Mohan
  Kriti
  Salim

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim", "Mohan", "Kriti", "Mohan"]

Below is how the values are positioned in the Array,

java_Collections

And as we can see, Mohan is present 3 times and Kriti is present 2 times in the Array.


And let's say, we don't want duplicate entries. We want Mohan and Kriti to be present just once.


So, to remove duplicate items, we have used the uniq() method.


y = x.uniq()

That removes all the duplicate items and assigns the new values to a new array y


Now, if you look at the new array y. All the duplicate elements are removed from it.

java_Collections

But just remember, the array x is not changed. It has all the duplicates present in it.