본문 바로가기
알고리즘/Array

Contains Duplicate

by e-pd 2020. 11. 20.

leetcode.com/problems/contains-duplicate/

 

Contains Duplicate - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
 
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        boolean isExisted = false;
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return isExisted = true;
            } else {
                map.put(nums[i], i);
            }
        }
        return isExisted;
        
    }
}
cs

 

'알고리즘 > Array' 카테고리의 다른 글

  (0) 2020.11.21
Best Time to Buy and Sell Stock  (0) 2020.11.20
Two Sum II  (0) 2020.11.19
나누어 떨어지는 숫자 배열  (0) 2020.09.17
x만큼 간격이 있는 n개의 숫자  (0) 2020.09.17