Jeremy Woertink wrote:
I actually had to ... find all the duplicate account
numbers and the number of times they were duplicated and ... .
...
~Jeremy
A much less verbose 'nil' fix of the original version would be to use
[v] instead of v:
a = [nil,1,2,2,3,nil]
p a.uniq.map {|v| (a - [v]).size < (a.size - 1) ? [v] :
nil}.compact.flatten
=> [nil, 2]
This fix does not work for a = [nil,1,2,[7],2,[7],3,nil], but the
previous version using "(a.size - a.nitems > 1) ? ..." does. Ruby 1.9
though is said to introduce a non-greedy Array#flatten:
# Ruby 1.9
a = [nil,1,[7],2,2,[7],3,nil]
p a.uniq.map {|v| (a - [v]).size < (a.size - 1) ? [v] :
nil}.compact.flatten(1)
=> [nil, [7], 2]
On Aug 19, 5:16 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 19.08.2007 23:15, Robert Klemme wrote:
>
>
>
> > On 19.08.2007 12:38, Thibaut Barrère wrote:
> >> Hi!
>
> >> Just wondering if there is something simple already built in the std
> >> library to remove duplicates from an array (or an enumerable). I've
> >> seen and used various approaches, like:
>
> >> module Enumerable
> >> def dups
> >> inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys
> >> end
> >> end
>
> >> which will give:
>
> >>> %w(a b c c).dups
> >> => ["c"]
>
> > Actually you are not deleting duplicates as far as I can see.
>
> Did I say it's too late? Man, I should've worn my glasses...
>
> > Here's another one
>
> > irb(main):012:0> a.inject(Hash.new(0)) {|h,x|
> > h+=1;h}.inject(){|h,(k,v)|h<<k if v>1;h}
> > => ["c"]
>
> > You could even change that to need just one iteration through the
> > original array but it's too late and I'm too lazy.
>
> Cheers
>
> robert
a = [1,2,3,2,1]
b = a & a
b = [1,2,3]
---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est man alive
...but that's not what the OP wanted. What you've written is the same
as the #uniq method.
Don't feel bad, this thread has been filled with people answering the
wrong question. The original question was roughly "How do I find
out all the elements in the array that are duplicates?"
Solutions to that question would not include '3' in the above results.
It's unclear to me if %w| a b b b | should include 'b' once or twice
in the output, though, and the original poster has not clarified that,
that I can see.
···
On Aug 21, 10:04 am, Ari Brown <a...@aribrown.com> wrote:
...but that's not what the OP wanted. What you've written is the same
as the #uniq method.
Don't feel bad, this thread has been filled with people answering the
wrong question. The original question was roughly "How do I find
out all the elements in the array that are duplicates?"
Solutions to that question would not include '3' in the above results.
It's unclear to me if %w| a b b b | should include 'b' once or twice
in the output, though, and the original poster has not clarified that,
that I can see.
I think once, since it's just the quality of being non-unique in
the array that qualifies an object for inclusion. At least, that's my
understanding, though as one of the people who reimplemented
Array#uniq, I may not be the right person to listen to