Last active
December 12, 2015 07:58
-
-
Save longlostnick/4740369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.