# Grouping values of a hash or array

**URL:** <https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421>\
**Category:** Ruby\
**Created:** [August 4, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421 "2006-08-04T20:02:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [August 4, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421/1 "2006-08-04T20:02:52Z")

</div>

If I’ve got a hash

> h = { a =\> 1, b =\> 2, c =\> 1, d =\> 1 }  
> Is there an easy way to group the values to come out as 1,2?

Same for an away where [1,2,1,2,3] would come out as 1,2,3. I looked  
through Ruby API and couldn’t find anything. Anyone have any suggestions  
as to how to go about this?

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [August 4, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421/2 "2006-08-04T20:15:54Z")

</div>

Adrian F. wrote:

> If I’ve got a hash
> 
> > h = { a =\> 1, b =\> 2, c =\> 1, d =\> 1 }  
> > Is there an easy way to group the values to come out as 1,2?
> 
> Same for an away where [1,2,1,2,3] would come out as 1,2,3  
> ^^^^  
> You’re from the Elmer Fudd school of Ruby programming apparently. 😛

> I looked  
> through Ruby API and couldn’t find anything. Anyone have any suggestions  
> as to how to go about this?

See Array#uniq

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [August 4, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421/3 "2006-08-04T20:24:53Z")

</div>

😃 Thanks for the help!

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [August 4, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/grouping-values-of-a-hash-or-array/101421/4 "2006-08-04T21:46:07Z")

</div>

Adrian F. wrote:

> If I’ve got a hash
> 
> > h = { a =\> 1, b =\> 2, c =\> 1, d =\> 1 }  
> > Is there an easy way to group the values to come out as 1,2?

irb(main):004:0\> require ‘set’  
=\> true  
irb(main):006:0\> { :a =\> 1, :b =\> 2, :c =\> 1, :d =\> 1 }.values.to\_set  
=\> #\<Set: {1, 2}\>  
irb(main):007:0\> { :a =\> 1, :b =\> 2, :c =\> 1, :d =\> 1  
}.values.to\_set.to\_a  
=\> [1, 2]

> Same for an away where [1,2,1,2,3] would come out as 1,2,3. I looked  
> through Ruby API and couldn’t find anything. Anyone have any suggestions  
> as to how to go about this?

Same as above - or use uniq as has been mentioned already.

```
robert
```
