LeetCode 存在重复

给定一个整数数组,判断是否存在重复元素。

如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。

示例 1:

输入: [1,2,3,1]
输出: true

示例 2:

输入: [1,2,3,4]
输出: false

示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

思路:Hashmap的简单应用,
如果已经hash过了就直接返回真,否则就进行hash
 1 public boolean containsDuplicate(int[] nums) {
 2         HashMap<Integer, Integer> m1 = new HashMap<Integer, Integer>();
 3         
 4         for(int i=0;i<nums.length;i++)
 5         {
 6             if(m1.containsKey(nums[i]))
 7             {
 8                 return true;
 9             }
10             else m1.put(nums[i], 1);
11         }
12         return false;
13     }
View Code
原文地址:https://www.cnblogs.com/tijie/p/10055473.html