Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-05-18_14-47-24.png
54.3 KB
#medium
#N260. Single Number III
problem link
#solution
class Solution {
public int[] singleNumber(int[] nums) {
int single1=0, single2=0;
boolean found=false;
Map<Integer, Integer> map = new HashMap<>();
for(int num: nums){
map.put(num, map.getOrDefault(num, 0) +1);
}
for(Map.Entry<Integer, Integer> entry: map.entrySet()){
if(entry.getValue()==1&&!found){
single1=entry.getKey();
found=true;
}
if(entry.getValue()==1&&found){
single2=entry.getKey();
}
}
return new int[]{single1, single2};
}
}