이중 for문은 당연히 시간 초과날 것 같아서 건너뛰고,
1트 => 새로운 배열 선언하고 해당 인덱스에 하나씩 카운트하기 + 1이상이면 return true => 음수 이슈 발생 !! (음수 인덱스X)
2트 => 배열 돌며 마이너스 제거 버전으로 배열 복제 + 카운트 + 1이상이면 return true
import java.util.*;
class Solution {
public boolean containsDuplicate(int[] nums) {
int N = nums.length;
int[] exceptNums = new int[N];
for(int i=0;i<N;i++){
if(nums[i]<0) exceptNums[i] = nums[i]*-1;
else exceptNums[i] = nums[i];
}
int[] cnt = new int[Arrays.stream(exceptNums).max().getAsInt()+1];
for(int i=0;i<N;i++){
cnt[exceptNums[i]]++;
if(cnt[exceptNums[i]]>1){
return true;
}
}
return false;
}
}
=> Wrong Answer (마이너스 제거해버리면 양수와 혼동 발생)
import java.util.*;
class Solution {
public boolean containsDuplicate(int[] nums) {
int N = nums.length;
List<Integer> arr = new ArrayList<>();
for(int i=0;i<N;i++){
if(arr.contains(nums[i])) return true;
arr.add(nums[i]);
}
return false;
}
}
=> 시간초과 발생
import java.util.*;
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> numSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
if(numSet.size()!=nums.length) return true;
else return false;
}
}
=> 통과