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 @   AaronTanooo  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示