Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Ruby - 2 dimension array search

I have the following 2 dimension array

data = [
 [5014031, nil], [5014032, nil], [5014033, 0], 
 [5014034, nil], [3014035, 1], [5014036, 1], 
 [5014037, 2], [5014038, nil], [5014039, 2], 
 [5014040, nil], [2014041, nil], [3014042, 2]
]

When I know the value of the 1st integer of one of these arrays e.g. 5014034, what would be the most efficient way to gain the next integer value where the 2nd element matches to nil? (e.g. I would expect 5014038 to be returned)

Thanks Scott

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Simple way, using #drop_while and #find_index:

data = [
  [5014031, nil], [5014032, nil], [5014033, 0],
  [5014034, nil], [5014035, 1], [5014036, 1],
  [5014037, 2], [5014038, nil], [5014039, 2],
  [5014040, nil], [5014041, nil], [5014042, 2]
]

remaining_arr = data.drop_while { |arr| arr[0] != 5014034 }[1 .. -1]
next_int = remaining_arr[remaining_arr.find_index { |arr| arr[1].nil? }][0]
over 4 years ago · Santiago Trujillo Report

0

You could slice the array into two when the first element in the inner array matches 5014034. Extract the second resulting array and use find to look for the inner array whose second element is nil:

arr
  .slice_when { |a, _| a == 5014034 } # #<Enumerator: ...>
  .to_a                               # [[[5014031, nil], [5014032, nil], [5014033, 0], [5014034, nil]], [[5014035, 1], [5014036, 1], [5014037, 2], [5014038, nil], [5014039, 2], [5014040, nil], [5014041, nil], [5014042, 2]]]
  [1]                                 # [[5014035, 1], [5014036, 1], [5014037, 2], [5014038, nil], [5014039, 2], [5014040, nil], [5014041, nil], [5014042, 2]]
  .find { |_, b| b.nil? }             # [5014038, nil]
  [0]                                 # 5014038

Notice this method chaining might fail if there's no array in arr whose first object inside matches 5014034, and/or the same for find.

over 4 years ago · Santiago Trujillo Report

0

If sorted then the following might be faster

data = [
 [5014031, nil], [5014032, nil], [5014033, 0], 
 [5014034, nil], [5014035, 1], [5014036, 1], 
 [5014037, 2], [5014038, nil], [5014039, 2], 
 [5014040, nil], [5014041, nil], [5014042, 2]
]

start_element = data.bsearch_index{ |a| a[0] >= 5014034 }

if start_element == nil || data[start_element] != 5014034
  puts "not found"
else
  data.slice((start_element+1)..).find { |a| a[1] == nil }[0]
end
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!