diff options
Diffstat (limited to 'find_lonely_numbers_array.py')
-rw-r--r-- | find_lonely_numbers_array.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/find_lonely_numbers_array.py b/find_lonely_numbers_array.py new file mode 100644 index 0000000..fa4706d --- /dev/null +++ b/find_lonely_numbers_array.py @@ -0,0 +1,25 @@ +# 2150. Find All Lonely Numbers in the Array +from collections import Counter + +""" +you are given an integer array 'nums'. a number 'x' is lonely when it appears +only once, and no adjacent numbers (ie. 'x + 1' and 'x - 1') appear in the +array. return all lonely numbers in 'nums'. you may return the answer in any +order +""" + + +class Solution(object): + def findLonely(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + m = Counter(nums) + return [n for n in nums if m[n] == 1 and m[n - 1] + m[n + 1] == 0] + + +if __name__ == "__main__": + obj = Solution() + print(obj.findLonely(nums=[10, 6, 5, 8])) + print(obj.findLonely(nums=[1, 3, 5, 3])) |