# Mode

**URL:** <https://www.ruby-forum.com/t/mode/95522>\
**Category:** Ruby\
**Created:** [June 5, 2006, 8:05am UTC](https://www.ruby-forum.com/t/mode/95522 "2006-06-05T08:05:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 5, 2006, 8:05am UTC](https://www.ruby-forum.com/t/mode/95522/1 "2006-06-05T08:05:21Z")

</div>

I bet there’s a more elegant way to write this. Anyone want to show me?

def mode(li)  
best\_num = nil  
best\_count = -1  
li.uniq.each do |item|  
count = (li.select { |item2| item == item2}).length  
if count \> best\_count  
best\_count = count  
best\_num = item  
end  
end  
best\_num  
end

– Elliot T.

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 5, 2006, 8:32am UTC](https://www.ruby-forum.com/t/mode/95522/2 "2006-06-05T08:32:29Z")

</div>

On 6/5/06, Elliot T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```

li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 5, 2006, 8:41am UTC](https://www.ruby-forum.com/t/mode/95522/3 "2006-06-05T08:41:52Z")

</div>

> -----Original Message-----  
> From: Elliot T. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> I bet there’s a more elegant way to write this. Anyone want  
> to show me?
> 
> def mode(li)  
> …  
> end

It would probably help if you said what you were trying to achieve  
rather than asking people to try and interpret code that you admit isn’t  
elegant.

class Array  
def mode  
count = Hash.new(0)  
each {|x| count[x] += 1 }  
count.sort\_by{|k,v|v}.last[0]  
end  
end

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 5, 2006, 9:24am UTC](https://www.ruby-forum.com/t/mode/95522/4 "2006-06-05T09:24:52Z")

</div>

On Jun 4, 2006, at 11:31 PM, Alder G. wrote:

> li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last

cool, thanks, collecting pairs works well.

On Jun 4, 2006, at 11:40 PM, Daniel S. wrote:

> It would probably help if you said what you were trying to achieve  
> rather than asking people to try and interpret code that you admit  
> isn’t  
> elegant.

Mode means:

(Statistics) the value that occurs most frequently in a given set of  
data.

> class Array  
> def mode  
> count = Hash.new(0)  
> each {|x| count[x] += 1 }  
> count.sort\_by{|k,v|v}.last[0]  
> end  
> end

that’s a nice way 🙂

– Elliot T.

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 6, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/mode/95522/5 "2006-06-06T14:55:30Z")

</div>

Hi Micheal,

I could not get your example to work properly. It seemed to return the  
count of the highest number in my initial list.

I modified it to

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.invert.max.last

and it gave me the result I expected. Is it just me tho?

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 6, 2006, 10:23am UTC](https://www.ruby-forum.com/t/mode/95522/6 "2006-06-06T10:23:14Z")

</div>

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.max.last

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 6, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/mode/95522/7 "2006-06-06T16:08:39Z")

</div>

> count of the highest number in my initial list.
> 
> I modified it to
> 
> li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.invert.max.last
> 
> and it gave me the result I expected. Is it just me tho?

another one:

li.sort\_by{|x| li.select{|y| x==y}.length}.last

cheers

Simon

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 6, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/mode/95522/8 "2006-06-06T16:28:29Z")

</div>

> another one:
> 
> li.sort\_by{|x| li.select{|y| x==y}.length}.last

even better (if === does what you expect)

li.sort\_by{|x|li.grep(x).size}.last

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [June 6, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/mode/95522/9 "2006-06-06T16:48:21Z")

</div>

Hi,

“Kroeger, Simon (ext)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> li.sort\_by{|x|li.grep(x).size}.last

In 1.9, you can use Enumerable#max\_by.

li.max\_by{|x|li.grep(x).size}
