LeetCode——Single Number

时间:2021-11-27 14:57:08

Description:

Given an array of integers, every element appears twice except for one. Find that single one.

找出只出现一次的数。

public class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0; i<nums.length; i++) {
if(map.containsKey(nums[i])) {
int times = map.get(nums[i]);
map.put(nums[i], times+1);
}
else
map.put(nums[i], 1);
}
Set<Integer> set = map.keySet();
for(int i : set) {
if(map.get(i) == 1) {
return i;
}
}
return 0; }
}