Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
Created July 2, 2013 19:47
Show Gist options
  • Select an option

  • Save riyadparvez/5912494 to your computer and use it in GitHub Desktop.

Select an option

Save riyadparvez/5912494 to your computer and use it in GitHub Desktop.
Find one missing number from an unsorted array of consecutive numbers in C#.
public static int FindMissingNumber(IEnumerable<int> numbers)
{
int expectedSum = (numbers.Count() + 1)*(numbers.Count()+2)/2;
int realSum = 0;
foreach (var number in numbers)
{
realSum += number;
}
return (expectedSum-realSum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment