# 2150. Find All Lonely Numbers in the Array ###### tags: `Leetcode` `Medium` `HashMap` Link: https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/description/ ## Code ```java= class Solution { public List<Integer> findLonely(int[] nums) { Map<Integer, Integer> cnt = new HashMap<>(); for(int num:nums){ cnt.put(num, cnt.getOrDefault(num, 0)+1); } List<Integer> ans = new ArrayList<>(); for(int num:nums){ if(cnt.get(num)==1 && !cnt.containsKey(num-1) && !cnt.containsKey(num+1)){ ans.add(num); } } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up