Leetcode Contains Duplicate

来源:互联网 发布:网络电视能连手机吗 编辑:程序博客网 时间:2024/06/16 14:50

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.


Difficulty: Easy


Solution 1: Sort

public class Solution {    public boolean containsDuplicate(int[] nums) {        Arrays.sort(nums);        for(int i = 1; i < nums.length; i++){            if(nums[i] == nums[i-1])                return true;        }        return false;    }}


Solution 2 : HashSet

public class Solution {    public boolean containsDuplicate(int[] nums) {        HashSet<Integer> set = new HashSet<Integer>();        for(int i = 0; i < nums.length; i++){            if(set.contains(nums[i]))                return true;            set.add(nums[i]);        }        return false;    }}




0 0
原创粉丝点击