You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
449 B
Ruby

#Given an array of length n contains elements from 0 to n-1,
#Print all the duplicate elements in O(n) time and constant space.
def print_duplicates(a)
n=a.length
print "Duplicate elements are: "
for i in 0...n
if a[a[i].abs]<0
print "#{a[i].abs} "
else
a[a[i].abs]= -a[a[i].abs]
end
end
return
end
print_duplicates([1, 4, 6, 4, 3, 6, 1]) # => Duplicate elements are: 4 6 1