Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Ruby Hash – Addition, Deletion, Selection problem solution

YASH PAL, 31 July 202429 January 2026

In this HackerRank Ruby Hash – Addition, Deletion, Selection problem solution we will show you ways in which we can add key-value pairs to Hash objects, delete keys from them, and retain them based on logic.

Consider the following Hash object:

h = Hash.new

h.default = 0

A new key-value pair can be added using or the store method

h[key] = value

or

h.store(key, value)

An existing key can be deleted using the delete method

h.delete(key)

For destructive selection and deletion, we can use keep_if and delete_if as seen in Array-Selection

> h = {1 => 1, 2 => 4, 3 => 9, 4 => 16, 5 => 25}

 => {1 => 1, 2 => 4, 3 => 9, 4 => 16, 5 => 25}

> h.keep_if {|key, value| key % 2 == 0} # or h.delete_if {|key, value| key % 2 != 0}

 => {2 => 4, 4 => 16}

Note

For non-destructive selection or rejection, we can use select, reject, and drop_while similar to Array-Selection

In this challenge, a hash object called hackerrank is already created. You have to add

A key-value pair [543121, 100] to the hackerrank object using store

Retain all key-value pairs where keys are Integers ( clue : is_a? Integer )

Delete all key-value pairs where keys are even-valued.

HackerRank Ruby Hash - Addition, Deletion, Selection problem solution

HackerRank Ruby Hash – Addition, Deletion, Selection problem solution.

# Enter your code here. 
hackerrank.store(543121,100)
hackerrank.keep_if {|a,b|  a.is_a? Integer }
hackerrank.delete_if {|a,b| a.even? }

Second solution.

# Enter your code here. 
hackerrank.store(543121, 100)
hackerrank.keep_if { | k, v| k.is_a? Integer }
hackerrank.delete_if { | k, v| k % 2 == 0 }

coding problems solutions Hackerrank Problems Solutions Ruby Solutions HackerRankruby

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes