217.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) {
            HashMap<Integer, Integer> map = new HashMap<>(); 
    for(int i=0;i<nums.length;i++){
    if(!map.containsKey(nums[i])){
    map.put(nums[i], 1);
    }else{
    return true ;
    }
    }
    return false ;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章