current position:Home>LeetCode 217. There are duplicate elements

LeetCode 217. There are duplicate elements

2022-05-15 02:48:12Amiad-D

Give you an array of integers nums . If any value appears in the array At least twice , return true ; If each element in the array is different from each other , return false .
 

Example 1:

Input :nums = [1,2,3,1]
Output :true
Example 2:

Input :nums = [1,2,3,4]
Output :false
Example  3:

Input :nums = [1,1,1,3,3,4,3,2,4,2]
Output :true
 

Tips :

1 <= nums.length <= 105
-109 <= nums[i] <= 109

source : Power button (LeetCode)
link :https://leetcode.cn/problems/contains-duplicate
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

Answer key :

class Solution {
    public static boolean containsDuplicate(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        for (int value: map.values()
             ) {
            if (value > 1) return true;
        }
        return false;
    }
}

copyright notice
author[Amiad-D],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120514340067.html

Random recommended