Find that number is repeated more and less times in an array

Hi all.

I’m trying to find a function to find the number that is repeated over
and the least is repeated within an array. I tried a lot of things but I
can not make it work.

For example:

a = [1 2 2 3 5 4 6 2 6 5 4]

I calculate the number that is repeated over and less repeats and if
possible to calculate something like this for example:

=> With 0 repeats found the number 1.3, etc.
=> With 1 repetition was found the number: 4, etc.
=> With 2 repetitions was found the number 5, etc.

Thanks.

Hi,

I don’t understand what you’re saying.

OK, you have a given number n. Are you looking for all elements that
occur at least n times in the array?

To count the number of occurences, you can use Enumerable#inject:

array = [1, 2, 2, 3, 2, 5]
element = 2
element_count = array.inject 0 do |count, x|
count + (x == element ? 1 : 0)
end

Or a more lowlevel solution:

array = [1, 2, 2, 3, 2, 5]
element = 2
element_count = 0
array.each do |x|
element_count += 1 if x == element
end

On Sun, Jul 8, 2012 at 8:32 AM, Joao S. [email protected] wrote:

=> With 0 repeats found the number 1.3, etc.
=> With 1 repetition was found the number: 4, etc.
=> With 2 repetitions was found the number 5, etc.

fire up irb or pry, and play w ruby,
eg,

a. group_by{|x|x}.group_by{|x,y| y.size-1}.each_with_object({}){|(k,v),h| h[k] =
v.flatten.uniq}

=> {0=>[1, 3], 2=>[2], 1=>[5, 4, 6]}

forgot the others:

a.uniq.group_by{|x|a.count(x)-1}

is the best

Brian C. wrote in post #1067902:

a = [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
counts = Hash.new(0)
a.each { |val| counts[val] += 1 }
groups = {}
counts.each do |val,count|
groups[count] ||= []
groups[count] << val
end
groups.each do |count,vals|
puts “With #{count-1} repeats found #{vals.sort.join(”, “)}”
end

One last thing, how do I print the results that I stay in order, because
the above code prints:

With 0 repeats found 1, 3
With 2 repeats found 2
With 1 repeats found 4, 5, 6

and I seek it to print in order:

With 0 repeats found 1, 3
With 1 repeats found 4, 5, 6
With 2 repeats found 2

Joao S. wrote in post #1067849:

a = [1 2 2 3 5 4 6 2 6 5 4]

I calculate the number that is repeated over and less repeats and if
possible to calculate something like this for example:

=> With 0 repeats found the number 1.3, etc.
=> With 1 repetition was found the number: 4, etc.
=> With 2 repetitions was found the number 5, etc.

Do it in steps. First count the number of times each value is seen:

a = [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
counts = Hash.new(0)
a.each { |val| counts[val] += 1 }

optional: counts = counts.sort_by { |val,count| count }

counts.each do |val,count|
puts “With #{count-1} repeats found the number #{val}”
end

This is a starting point. Then you can look at grouping together values
with the same count.

a = [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
counts = Hash.new(0)
a.each { |val| counts[val] += 1 }
groups = {}
counts.each do |val,count|
groups[count] ||= []
groups[count] << val
end
groups.each do |count,vals|
puts “With #{count-1} repeats found #{vals.sort.join(”, “)}”
end

When you’re clear what’s happening here then you can look at the more
compact versions using group_by.

On Sun, Jul 8, 2012 at 10:13 PM, Joao S. [email protected]
wrote:

groups.each do |count,vals|

and I seek it to print in order:

groups.sort.each do |count,vals|
puts “With #{count-1} repeats found #{vals.sort.join(”, “)}”
end

With 0 repeats found 1, 3
With 1 repeats found 4, 5, 6
With 2 repeats found 2

I think you should read a little bit about classes Array, Hash and
module Enumerable, and try yourself things around in IRB until you
feel more comfortable about how to use many of the facilities they
provide.

Jesus.

On Mon, Jul 9, 2012 at 9:14 AM, Jess Gabriel y Galn
[email protected] wrote:

end
With 1 repeats found 4, 5, 6

and I seek it to print in order:

groups.sort.each do |count,vals|
puts “With #{count-1} repeats found #{vals.sort.join(”, “)}”
end

Alternative solution

irb(main):001:0> a = [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
=> [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
irb(main):002:0> max=0; counts = a.inject(Hash.new(0)) {|c, x|
cc=(c[x]+=1); max=cc if cc>max; c}
=> {1=>1, 2=>3, 3=>1, 5=>2, 4=>2, 6=>2}
irb(main):003:0> 1.upto(max){|r| printf “With %d repeats: %p\n”, r,
counts.select {|k,v| v==r}.map{|k,v|k} }
With 1 repeats: [1, 3]
With 2 repeats: [5, 4, 6]
With 3 repeats: [2]
=> 1

(Explanation omitted for pedagogical reasons.)

Have fun!

robert