217. Contains Duplicate [Easy]

217. Contains Duplicate

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Constraints:

  • -1000 <= a, b <= 1000

Example

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

思路

有无重复,是否存在 ->HashSet
出现次数 -> HashMap

题解

    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> container = new HashSet<>();
        for (int val : nums) {
            if (container.contains(val))
                return true;
            container.add(val);
        }
        return false;
    }
posted @ 2023-01-09 11:26  AaronTanooo  阅读(23)  评论(0)    收藏  举报