-
[Algorithm] LeetCode 217.contains-duplicateAlgorithm 2023. 10. 10. 23:20반응형
217. Contains Duplicate
Easy
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example 1:
Input: nums = [1,2,3,1] Output: true
Example 2:
Input: nums = [1,2,3,4] Output: false
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true
Constraints:
- 1 <= nums.length <= 105
- -109 <= nums[i] <= 109
solution:
class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> numsHash = new HashSet<>(); for (int i = 0; i < nums.length; i++) { numsHash.add(nums[i]); if (numsHash.size() != i + 1) return true; } return false; } }
반응형'Algorithm' 카테고리의 다른 글
[Algorithm] LeetCode 53. Maximum Subarray (3) 2023.10.12 [Algorithm] LeetCode 238.product-of-array-except-self (0) 2023.10.10 [Algorithm] LeetCode 121. Best Time to Buy and Sell Stock (0) 2023.09.30 [LeetCode] 추천 75 문제 및 알고리즘 공부 방법 (0) 2023.09.30 [Algorithm] LeetCode 7. Reverse Integer (0) 2023.09.26