# Distinct

**URL:** <https://www.ruby-forum.com/t/distinct/91826>\
**Category:** Ruby\
**Created:** [April 12, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/distinct/91826 "2007-04-12T17:55:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [April 12, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/distinct/91826/1 "2007-04-12T17:55:34Z")

</div>

hello,

a little question… how to get distinct values from an hash?

ex:  
hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
==\> hash\_with\_distinct\_values = [100, 200]

thanks.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [April 12, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/distinct/91826/2 "2007-04-12T17:57:35Z")

</div>

Jean-Sébastien wrote:

> hello,
> 
> a little question… how to get distinct values from an hash?
> 
> ex:  
> hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
> ==\> hash\_with\_distinct\_values = [100, 200]  
> hash.values.uniq is the simplest.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [April 12, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/distinct/91826/3 "2007-04-12T20:21:54Z")

</div>

On Fri, Apr 13, 2007 at 12:55:06AM +0900, Jean-S?bastien wrote:

> a little question… how to get distinct values from an hash?
> 
> ex:  
> hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
> ==\> hash\_with\_distinct\_values = [100, 200]

irb(main):001:0\> hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
=\> {“a”=\>100, “b”=\>200, “d”=\>200}  
irb(main):002:0\> hash.invert.keys  
=\> [100, 200]

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [April 12, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/distinct/91826/4 "2007-04-12T21:11:00Z")

</div>

On Apr 12, 11:54 am, “Jean-Sébastien” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hello,
> 
> a little question… how to get distinct values from an hash?
> 
> ex:  
> hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
> ==\> hash\_with\_distinct\_values = [100, 200]
> 
> thanks.

irb(main):001:0\> hash = {“a”=\>100, “b”=\>200, “d”=\>100, “d”=\>200}  
=\> {“a”=\>100, “b”=\>200, “d”=\>200}  
irb(main):002:0\> hash.values.uniq  
=\> [100, 200]

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [April 12, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/distinct/91826/5 "2007-04-12T21:33:04Z")

</div>

On 4/12/07, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> [100, 200]

I would have completely agreed with hash.values.uniq as the simplest  
solution, but has.invert.keys is extremely surprising, thx Brian.

Robert
