|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=217 lang=java |
| 3 | + * |
| 4 | + * [217] Contains Duplicate |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/contains-duplicate/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Easy (56.60%) |
| 10 | + * Total Accepted: 749.2K |
| 11 | + * Total Submissions: 1.3M |
| 12 | + * Testcase Example: '[1,2,3,1]' |
| 13 | + * |
| 14 | + * Given an array of integers, find if the array contains any duplicates. |
| 15 | + * |
| 16 | + * Your function should return true if any value appears at least twice in the |
| 17 | + * array, and it should return false if every element is distinct. |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * |
| 21 | + * |
| 22 | + * Input: [1,2,3,1] |
| 23 | + * Output: true |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * |
| 27 | + * |
| 28 | + * Input: [1,2,3,4] |
| 29 | + * Output: false |
| 30 | + * |
| 31 | + * Example 3: |
| 32 | + * |
| 33 | + * |
| 34 | + * Input: [1,1,1,3,3,4,3,2,4,2] |
| 35 | + * Output: true |
| 36 | + * |
| 37 | + */ |
| 38 | +class Solution { |
| 39 | + public boolean containsDuplicate(int[] nums) { |
| 40 | + |
| 41 | + HashSet<Integer> set = new HashSet<Integer>(); |
| 42 | + |
| 43 | + for (int i = 0; i < nums.length; ++i) { |
| 44 | + if (set.contains(nums[i])) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + set.add(nums[i]); |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | + } |
| 52 | +} |
0 commit comments