Skip to content

Instantly share code, notes, and snippets.

@longlostnick
Last active December 12, 2015 07:58
Show Gist options
  • Save longlostnick/4740369 to your computer and use it in GitHub Desktop.
Save longlostnick/4740369 to your computer and use it in GitHub Desktop.
def print_repeating(array)
0.upto(array.size - 1) do |i|
flag = array[array[i].abs]
flag = 1 if flag.nil?
if flag >= 0
array[array[i].abs] = -flag
else
puts array[i].abs
end
end
end
@longlostnick
Copy link
Author

This algorithm works for any size integers, but it will only work in languages with dynamic arrays. Otherwise one could use a hash map with fixed memory size n.

If you know all of your integers are 0 <= x < n you don't need #6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment