One needs to have a look at the whole array first to decide the uniqueness
def one(a)
o = { }
a.each do |x|
v = o[x]
if v == nil
o[x] = true
else
if v
o[x] = false
end
end
end
return o
end
and then use this to pick the unique elements
def unique(a)
o = one(a)
b = [ ]
o.each do |k, v|
if v
b.push(k)
end
end
return b
end
Test code
a = [ 1, 2, 3, 4, 4, 5, 1, 4, 3 ]
b = unique(a)
puts "unique: #{a} -> #{b}"
Output
unique: [1, 2, 3, 4, 4, 5, 1, 4, 3] -> [2, 5]
Dedicated to Edsger W. Dijkstra
A modern, competent programmer should not be puzzle-minded, he should not revel in tricks, he should be humble and avoid clever solutions like the plague
(from EWD303)