J
Jari Williamsson
I have an array that's sorted from highest value to lowest. I also want
to insert a value in sort order, but only if the value doesn't exist
already. I ended up with a solution (writing from memory here, it was on
another computer) that feels a bit "long" for Ruby code, going something
like this:
---
arr = [5, 3, 1]
insert_value = 2
arr.each_index {|v, i|
break if v == insert_value
if insert_value > v
arr.insert(i, insert_value)
break
end
}
---
Is there a "simpler" solution?
Best regards,
Jari Williamsson
to insert a value in sort order, but only if the value doesn't exist
already. I ended up with a solution (writing from memory here, it was on
another computer) that feels a bit "long" for Ruby code, going something
like this:
---
arr = [5, 3, 1]
insert_value = 2
arr.each_index {|v, i|
break if v == insert_value
if insert_value > v
arr.insert(i, insert_value)
break
end
}
---
Is there a "simpler" solution?
Best regards,
Jari Williamsson