William James
11/17/2015 5:01:00 PM
> Just wanted to confirm I hadn't missed any obvious solution before
> rolling my own version. Thanks for the replies!
>
> I ended up with something like:
>
> (defun duplicates (list)
> (let ((h (make-hash-table)))
> (iter (for x in list)
> (for i from 0)
> (cond ((gethash x h)
> (push i (gethash x h)))
> (t (setf (gethash x h) (list i)))))
> (iter (for (key value) in-hashtable h)
> (when (> (length value) 1)
> (collect (list key value))))))
>
> MSINGH> (duplicates '(1 2 4 4 5 x x y y z))
> ((4 (3 2)) (X (6 5)) (Y (8 7)))
MatzLisp (Ruby):
def duplicates ary
ary.each_with_index.group_by{|x,i| x}.
reject{|k,v| v.one?}.map{|k,v| [k, v.map(&:last)]}
end
duplicates %w(1 2 4 4 5 x y x y z)
==>[["4", [2, 3]], ["x", [5, 7]], ["y", [6, 8]]]
--
Arrogant and isolated, the elite treat the ordinary citizens of the empire as
slaves and potential enemies.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm