-
-
Save keaston/d4826239cd80b87551d0 to your computer and use it in GitHub Desktop.
Trying out an SO solution ...
This file contains 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
/** Trying out the solution to: | |
* http://stackoverflow.com/questions/5739024/finding-duplicates-in-on-time-and-o1-space | |
* Created by kmhaswade on 3/5/16. | |
* Updated by keaston 2016-03-07 | |
*/ | |
public class DuplicateFinder { | |
public static void main(String[] args) throws IOException { | |
int[] a = new int[]{3, 0, 1, 3, 2}; | |
int n = a.length; | |
for (int i = 0; i <= n-1; i++) { | |
System.out.println("i: " + i + ", a[i]: " + a[i] + ", a[a[i]]: " + a[a[i]]); | |
while (a[a[i]] != a[i]) { | |
int tmp = a[i]; | |
a[i] = a[tmp]; | |
a[tmp] = tmp; | |
System.out.println(Arrays.toString(a)); | |
System.in.read(); | |
} | |
} | |
for (int i = 0; i <= n-1; i++) | |
if (a[i] != i) | |
System.out.println(a[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment