[LC BC] Contains Duplicate 27

YJ·4일 전

LeetCode Bootcamp

목록 보기
3/6

https://www.hackerrank.com/contests/leetcode-bootcamp-week-1/challenges/contains-duplicate-27-2/problem?isFullScreen=true

문제

안에 중복 요소 있으면 true, 다 unique 하면 false

답안

public static boolean containsDuplicate(List<Integer> nums) {
	Set<Integer> set = new HashSet<>();
	for (int num: nums) {
		int beforeSize = set.size();
		set.add(num);
		if (beforeSize == set.size()) {
			return true;
		}
	}
	return false;
}

메모

Set 으로 체크하면 So 간단..

profile
Hello

0개의 댓글