Skip to content

Instantly share code, notes, and snippets.

@davidbreyer
Created October 9, 2018 17:25
Show Gist options
  • Save davidbreyer/bf666204ffca41a8e19516ecd2d5a260 to your computer and use it in GitHub Desktop.
Save davidbreyer/bf666204ffca41a8e19516ecd2d5a260 to your computer and use it in GitHub Desktop.
int firstDuplicate(int[] a) {
for (int i = 0; i < a.Length; i++) {
if (a[Math.Abs(a[i])-1] >= 0)
a[Math.Abs(a[i])-1] = -a[Math.Abs(a[i])-1];
else
return Math.Abs(a[i]);
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment