![]()
package my;
import java.util.HashMap;
import java.util.Map;
public class SingleNumberSolution {
public int[] singleNumber(int[] nums) {
int[] result = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0 ;i<nums.length; i++){
if(map.containsKey(nums[i])){
map.put(nums[i],map.get(nums[i])+1);
}else{
map.put(nums[i],1);
}
}
int k=0;
for(int j=0; j< nums.length; j++){
if(map.get(nums[j]) ==1){
result[k++] = nums[j];
}
}
return result;
}
int[] singleNumber2(int[] nums){
Map<Integer,Integer> map = new HashMap<>();
for(int num: nums){
map.put(num,map.getOrDefault(num,0)+1);
}
int[] result = new int[2];
int k= 0;
for(Map.Entry<Integer,Integer> item : map.entrySet()){
if(item.getValue() == 1){
result[k++] = item.getKey();
}
}
return result;
}
public static void main(String[] args){
int[] nums ={1,2,1,3,2,5};
int[] sn =new SingleNumberSolution().singleNumber2(nums);
for(int s :sn){
System.out.println(s);
}
}
}