首页 > 其他分享 >217. Contains Duplicate [Easy]

217. Contains Duplicate [Easy]

时间:2023-01-09 11:35:16浏览次数:51  
标签:217 container val Contains Duplicate return

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.

思路

有无重复,是否存在 ->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;
    }

标签:217,container,val,Contains,Duplicate,return
From: https://www.cnblogs.com/tanhaoo/p/17035892.html

相关文章