Leetcode/Top Interview 150
Contains Duplicate
자전거통학
2021. 6. 2. 07:38
https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/578/
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
Note : Kinda easy one when we can use a hash-table type structure...like map or dictionary.
But it's interesting since using a List could not be an answer because it's timed out.
( List is kinda extended array. So the searching cannot be fast.! )
public bool ContainsDuplicate(int[] nums) {
// using dic.
Dictionary<int, int> dicTemp = new Dictionary<int, int>();
for(int k = 0; k < nums.Length; ++k)
{
if(dicTemp.ContainsKey( nums[k] ))
return true;
dicTemp.Add(nums[k], 0);
}
// using list = timeout.
/*List<int> listTemp = new List<int>();
for(int k = 0; k < nums.Length; ++k)
{
if(listTemp.Contains(nums[k]))
return true;
listTemp.Add(nums[k]);
}*/
return false;
}