LeetCode-Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if(nums == null){
            return false;
        }
        HashSet<Integer> set=new HashSet<Integer>();
        int len =nums.length;
        for(int i=0; i<len; i++){
            if(set.contains(nums[i])){
                return true;
            }
            else{
                set.add(nums[i]);
            }
        }
        return false;
    }
}

  

时间: 2024-08-25 02:13:34

LeetCode-Contains Duplicate的相关文章

[LeetCode] Remove Duplicate Letters 移除重复字母

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q

[LeetCode] Delete Duplicate Emails 删除重复邮箱

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------------+ | Id | Email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected]

[LeetCode][SQL]Duplicate Emails

https://leetcode.com/problems/duplicate-emails/ Duplicate Emails Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email

【Leetcode】Duplicate Emails

题目链接:https://leetcode.com/problems/duplicate-emails/ 题目: Write a SQL query to find all duplicate emails in a table named Person. +--+---+ | Id | Email | +--+---+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +--+---+ F

LeetCode:Duplicate Emails - 重复出现的Email

1.题目名称 Duplicate Emails(重复出现的Email) 2.题目地址 https://leetcode.com/problems/duplicate-emails/ 3.题目内容 有一个数据表包括Id和Email两列,找出数据表内Email列内容重复出现的Email数据. 例如,现有一个表Person内容如下: +----+---------+ | Id | Email   | +----+---------+ | 1  | [email protected] | | 2  | 

[leetcode] Remove Duplicate Letters

题目: Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bca

[LeetCode] Contains Duplicate II 包含重复值之二

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 这道题是之前那道Contains Duplicate 包含重复值的延伸,不同之处在于那道题只要我们

LeetCode - Delete Duplicate Emails

Discription:Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. 删除重复的Email地址,保留Id最小的那个. 使用自身连接循环即可. # Write your MySQL query statement below delete p1 from Person p1, P

leetcode 182. Duplicate Emails

传送门 182. Duplicate Emails My Submissions Question Total Accepted: 14498 Total Submissions: 38364 Difficulty: Easy Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email pr

[LeetCode] Contains Duplicate III 包含重复值之三

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 参考资料: https://leetcode.com/discuss/381