# Unique by id

**URL:** <https://www.ruby-forum.com/t/unique-by-id/187071>\
**Category:** Rails\
**Created:** [April 16, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/unique-by-id/187071 "2010-04-16T15:22:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 16, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/unique-by-id/187071/1 "2010-04-16T15:22:26Z")

</div>

Guyz, tell me one thing that if i have an array of object of type ABC…  
then how to find the objects from that array which are uniq in id… I  
mean unique by ABC.id…

May be a newbie question…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 16, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/unique-by-id/187071/2 "2010-04-16T17:36:16Z")

</div>

On 16 April 2010 14:22, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Guyz, tell me one thing that if i have an array of object of type ABC…  
> then how to find the objects from that array which are uniq in id… I  
> mean unique by ABC.id…

If you have two with the same id how do you know which one you want?

If you just mean that you want to strip out duplicate entries then  
look at then just use abc.uniq (Have a look at the docs for the Array  
class).

Colin

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 16, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/unique-by-id/187071/3 "2010-04-16T23:30:53Z")

</div>

Colin L. wrote:

> On 16 April 2010 14:22, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Guyz, tell me one thing that if i have an array of object of type ABC…  
> > then how to find the objects from that array which are uniq in id… I  
> > mean unique by ABC.id…
> 
> If you have two with the same id how do you know which one you want?
> 
> If you just mean that you want to strip out duplicate entries then  
> look at then just use abc.uniq (Have a look at the docs for the Array  
> class).

Depending on your requirements, another option is to use a Set instead  
of an Array. With a Set you won’t end up with duplicates in the first  
place.
