Skip to content

Instantly share code, notes, and snippets.

@kedarmhaswade
Created March 6, 2016 02:20
Show Gist options
  • Save kedarmhaswade/92fe0e0a1bdb0f35c6ab to your computer and use it in GitHub Desktop.
Save kedarmhaswade/92fe0e0a1bdb0f35c6ab to your computer and use it in GitHub Desktop.
Trying out an SO solution ...
/** 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.
* Have I translated @caf's pseudocode correctly?
*/
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[a[i]];
a[a[i]] = 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