Skip to content

Instantly share code, notes, and snippets.

@zillwc
Created February 22, 2016 06:01
Show Gist options
  • Select an option

  • Save zillwc/47f24438ee21eb7eb267 to your computer and use it in GitHub Desktop.

Select an option

Save zillwc/47f24438ee21eb7eb267 to your computer and use it in GitHub Desktop.
public class FindDuplicateNumber {
public static int summationRes(int[] array, int len) {
for (int i = 0; i < len; i++) {
array[array[i] % len] += len;
if (array[array[i] % len] / len == 2)
return array[i] % len;
}
return -999;
}
public static int cycleDetectAlgo(int[] array, int len) {
int fast = len - 1;
int slow = len - 1;
while (true) {
slow = array[slow];
fast = array[array[fast]];
if (slow == fast)
break;
}
return array[slow];
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 1, 4, 4};
System.out.println(cycleDetectAlgo(array, array.length)); // 4
System.out.println(summationRes(array, array.length)); // 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment