Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yohey03518/f1ace73d5abdd318e37b505ef0481d8b to your computer and use it in GitHub Desktop.
Save yohey03518/f1ace73d5abdd318e37b505ef0481d8b to your computer and use it in GitHub Desktop.
public bool ContainsDuplicate(int[] nums)
{
int count = nums.Count();
if (count == 0) return false;
int max = nums.Max();
int min = nums.Min();
int[] positiveRecord = new int[max + 1];
int[] negativeRecord = new int[(min < 0) ? Math.Abs(min) + 1 : 0];
for (int i = 0; i < count; i++)
{
if (nums[i] >= 0)
{
if (positiveRecord[nums[i]] == 1)
return true;
else
positiveRecord[nums[i]] = 1;
}
else
{
if (negativeRecord[nums[i] * -1] == 1)
return true;
else
negativeRecord[nums[i] * -1] = 1;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment