Method: Array#occurent
- Defined in:
- lib/core/facets/array/nonuniq.rb
#occurent(n = 2) ⇒ Object
Returns a list of elements that occur n times.
[0,1,1,1,3,0,1,2,4].occurent(3) #=> [1]
If n is a Range then returns elements that occur a number of time within the range.
[0,1,1,1,3,0,1,2,4].occurent(2..4) #=> [0,1]
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/core/facets/array/nonuniq.rb', line 57 def occurent(n=2) h = Hash.new( 0 ) each do |i| h[i] += 1 end case n when nil h.delete_if{ |_,v| ! yield(v) }.keys when Range h.delete_if{ |_,v| ! n.include?(v) }.keys else h.delete_if{|_,v| v != n}.keys end end |