-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
31 lines (26 loc) · 880 Bytes
/
Solution.cs
File metadata and controls
31 lines (26 loc) · 880 Bytes
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
namespace LeetCode.Problem136{
//136. Single Number
//https://leetcode.com/problems/single-number/
/*
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
*/
public class Solution {
public int SingleNumber(int[] nums) {
Queue<int> numbers = new Queue<int>(nums);
HashSet<int> removed = new HashSet<int>();
int ans = 0;
while (numbers.Count > 0)
{
int val = numbers.Dequeue();
if (!numbers.Contains(val) && !removed.Contains(val))
{
ans = val;
return val;
}
removed.Add(val);
}
return ans;
}
}
}