Given an array of integers, every element appearsthreetimes except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
class Solution {
public int singleNumber(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums){
set.add(num);
}
long sum1 = 0;
for (int num : set){
sum1 += num;
}
long sum2 = 0;
for (int num : nums){
sum2 += num;
}
return (int)((sum1 * 3 - sum2) / 2);
}
}