-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question1.cs
32 lines (32 loc) · 874 Bytes
/
Question1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Question1{
public static void Main1()
{
int[] nums = {2,2,1};
Console.Write(SingleNumber(nums));
}
public static int SingleNumber(int[] nums)
{
Array.Sort(nums);
if (nums.Length > 1)
{
for (int i = 1; i < nums.Length - 1; i++)//check in between first and last index
{
if (nums[i - 1] != nums[i] && nums[i + 1] != nums[i])
{
return nums[i];
}
}
if (nums[0] != nums[1])//first index is single
{
return nums[0];
}
if (nums[nums.Length - 1] != nums[nums.Length - 2])//last index is single
{
return nums[nums.Length - 1];
}
}
else
return nums[0];
return 0;
}
}