WoNáDo
5/11/2007 11:52:00 PM
Wolfgang Nádasi-Donner schrieb:
> Or is it something like this?
>
> ...
> --- before ---
> ["a", "b", "a", "a", "c", "a", "b", "c", "d", "e", "f", "e", "a"]
> ["a", "b", "a", "c", "d", "c", "d", "c", "c", "g", "h", "g", "c"]
> --- after ---
> ["a", "a", "b", "e", "f", "e", "a"]
> ["d", "c", "c", "g", "h", "g", "c"]
If it is the wanted direction, the program is simple.
class Array
def remdups(b)
a= self.dup
b.each{|el|a.delete_at(a.index(el)) if a.include?(el)}
a
end
end
a = %w{a b a a c a b c d e f e a}
b = %w{a b a c d c d c c g h g c}
puts '--- before ---'
p a
p b
puts '--- after ---'
p a.remdups(b)
p b.remdups(a)
p [1,1,1,1].remdups([1,1,2])
p [1,1,2].remdups([1,1,1,1])
Output:
--- before ---
["a", "b", "a", "a", "c", "a", "b", "c", "d", "e", "f", "e", "a"]
["a", "b", "a", "c", "d", "c", "d", "c", "c", "g", "h", "g", "c"]
--- after ---
["a", "a", "b", "e", "f", "e", "a"]
["d", "c", "c", "g", "h", "g", "c"]
[1, 1]
[2]
Wolfgang Nádasi-Donner