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
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Ruby Array – Selection problem solution

YASH PAL, 31 July 202429 January 2026

In this HackerRank Ruby Array – Selection problem solution The array class also allows to selection and return a subset of an array based on some criteria defined in a block (a block is a group of code within {} that accepts a variable and returns a value).

Selecting elements that satisfy a given criteria

Rejecting elements that satisfy a given criteria

 > arr = [3, 4, 2, 1, 2, 3, 4, 5, 6]

 > arr.select {|a| a > 2}

 => [3, 4, 3, 4, 5, 6]

 > arr.reject {|a| a > 2}

 => [2, 1, 2]

 > arr

 => [3, 4, 2, 1, 2, 3, 4, 5, 6]

 > arr.drop_while {|a| a > 1} # removes elements till the block returns false for the first time

 => [1, 2, 3, 4, 5, 6]

As you can see, the original array remains unchanged. This is called Non-Destructive Selection.

For destructive behavior (change to the original array), Ruby provides the following methods:

 > arr = [3, 4, 2, 1, 2, 3, 4, 5, 6]  

 > arr.delete_if {|a| a < 2}

  => [3, 4, 2, 2, 3, 4, 5, 6]  

 > arr.keep_if {|a| a < 4}  

 => [3, 2, 2, 3]

Note

An element in a block is selected, rejected, deleted, or kept based on the True or False value generated by that block on that element.

For a destructive behavior for select and reject or any method that one wants to enforce a change in the original array, a ! can be used at the end of the method i.e., select! and reject!

In this challenge, you have to complete the functions using syntax as explained above.

HackerRank Ruby Array - Selection problem solution

HackerRank Ruby Array – Selection problem solution.

def select_arr(arr)
    # select and return all odd numbers from the Array variable `arr`
    arr.select do |n|
        n.odd?
    end
    
end

def reject_arr(arr)
    # reject all elements which are divisible by 3
    arr.reject do |n|
        n % 3 == 0
    end
end

def delete_arr(arr)
    arr.delete_if do |n|
        n < 0
    end
    # delete all negative elements
end

def keep_arr(arr)
    arr.keep_if { |n| n >= 0 }
    # keep all non negative elements ( >= 0)
end
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