We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8901b1d commit a674237Copy full SHA for a674237
contains-duplicate/limlimjo.js
@@ -0,0 +1,23 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {boolean}
4
+ */
5
+var containsDuplicate = function(nums) {
6
+
7
+ // 첫 번째 방법: filter + indexOf 사용 => indexOf(),filter() 각각 시간복잡도 O(n) 두 개가 중첩이므로 시간복잡도 O(n^2)
8
+ // Runtime: Time Limit Exceeded 발생
9
+ const method1 = function() {
10
+ const filterNums = nums.filter((item,index) => nums.indexOf(item) !== index);
11
+ return filterNums.length > 0;
12
+ }
13
14
+ // 두 번째 방법: Set 사용 => nums 배열을 set으로 변환할 때 한번씩 확인하면 되므로 시간복잡도 O(n)
15
+ // Runtime: 14ms
16
+ const method2 = function() {
17
+ const setNums = new Set(nums);
18
+ return setNums.size !== nums.length;
19
20
21
+ // 위 두 가지 방법 중 Set을 사용하는 것이 성능상 훨씬 나음
22
+ return method2();
23
+};
0 commit comments