I have an array of objects with top and left attributes like this:
[{top: 30, left: 20}, {top:50, left:10}, {..}]
I am trying to find how many objects have the approximate same top value. In this case:
[{top: 10, left: 20}, {top:10, left:10}, {top: 10, left: 123},
{top:500, left:10}, {top:2, left: 50}, {top:2, left:400}]
the method would return 5
because there are three objects that have 10
as top value and two objects that have 2
as top value. If there is an object that doesn't share its top value with anything else, it is not taken into account. I am not looking for exact values, but with a flexibility of a 10% of difference on the values so that {top:10, left:20}
and {top:10.13, left:20}
would be considered as having the same top. So far, I have this:
myarr.group_by { |x| x[:top] }.map { |k,v| [k, v.length] }.to_h
but this would not take into account this margin error. I am not sure how I would change it in order to do so.