Skip to content

Commit 67de21f

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 8215351 + 32d80da commit 67de21f

File tree

193 files changed

+5191
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+5191
-59
lines changed

contains-duplicate/DaleSeo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::collections::HashSet;
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
impl Solution {
6+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
7+
let mut set = HashSet::new();
8+
!nums.into_iter().all(|num| set.insert(num))
9+
}
10+
}

contains-duplicate/Geegong.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
5+
public class Geegong {
6+
7+
8+
/**
9+
* time complexity : O(n)
10+
* space complexity : o(n)
11+
* @param nums
12+
* @return
13+
*/
14+
public boolean containsDuplicate(int[] nums) {
15+
HashSet<Integer> uniques = new HashSet<>();
16+
17+
for (int num : nums) {
18+
if (uniques.contains(num)) {
19+
return true;
20+
}
21+
22+
uniques.add(num);
23+
}
24+
25+
return false;
26+
}
27+
28+
}
29+

contains-duplicate/Grit03.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
const countMap = new Map();
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
const key = nums[i];
10+
if (countMap.has(key)) {
11+
const value = countMap.get(key);
12+
if (value === 1) {
13+
return true;
14+
}
15+
countMap.set(key, value + 1);
16+
} else {
17+
countMap.set(key, 1);
18+
}
19+
}
20+
21+
return false;
22+
};

contains-duplicate/Kyojin-Hwang.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
const map = new Map();
7+
8+
for (let num of nums) {
9+
map.set(num, (map.get(num) || 0) + 1);
10+
}
11+
12+
for (let [_, count] of map) {
13+
if (count > 1) return true;
14+
}
15+
16+
return false;
17+
};
18+
19+
// set 방식으로도 대체가능
20+
// var containsDuplicate = function(nums) {
21+
// return new Set(nums).size !== nums.length;
22+
// };

contains-duplicate/Lustellz.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
let tmp: number[] = nums.toSorted()
3+
for(let i = 0 ; i < nums.length - 1 ; i++){
4+
if(tmp[i]===tmp[i+1]) return true
5+
}
6+
return false
7+
};

contains-duplicate/SamTheKorean.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC : O(n) : it iterates for the length of nums
2+
// SC : O(n) : hashmap is created with the size of nums
3+
4+
func containsDuplicate(nums []int) bool {
5+
hashmap := make(map[int]bool)
6+
7+
for i:=0;i<len(nums);i++ {
8+
if hashmap[nums[i]] {
9+
return true
10+
}
11+
12+
hashmap[nums[i]] = true
13+
}
14+
15+
return false
16+
}
17+

contains-duplicate/Yg-cho.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
return new Set(nums).size !== nums.length;
7+
};
8+
9+
//console.log(containsDuplicate([1, 2, 3, 1])); // true
10+
// console.log(containsDuplicate([1, 2, 3, 4])); // false
11+
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true

contains-duplicate/bskkimm.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def containsDuplicate(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: bool
7+
"""
8+
# [1,2,3,1]
9+
10+
# [1,1,1,3,3,4,3,2,4,2]
11+
12+
# add element to a dict
13+
# if a same value appears, then return True
14+
# if a for loop ends without disruption, return False
15+
16+
dict = defaultdict(bool)
17+
18+
for num in nums:
19+
if dict[num]:
20+
return True
21+
else:
22+
dict[num] = True
23+
24+
return False
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
contains-duplicate
3+
요구사항: 주어진 입력에서 중복된 원소가 존재하는지 여부를 참/거짓으로 반환.
4+
접근 1: 가장 단순한 방법은 다른 vector input_list를 두고, 입력 반복마다 전체 스캔해서 중복 여부를 체크하는 것입니다.
5+
input_list의 스캔 비용 O(N)을 모든 입력에 대해 반복하므로 총 시간 복잡도는 O(N^2)입니다.
6+
공간 복잡도는 입력과 같으므로 O(N)입니다.
7+
접근 2: 원소의 존재 여부를 더 빠르게 체크하는 방법은 set을 사용하는 것입니다.
8+
set은 중복 원소를 검출하는데 효과적인 자료구조로, 내부 구현에 따라 비용과 특성이 다릅니다.
9+
- C++에서 ordered_{set, map}은 정렬을 유지하는 search tree를 사용하므로 쿼리와 삽입, 삭제 비용이 평균 O(log(N))입니다.
10+
search tree의 구현에 따라 최악의 경우 쿼리 비용이 O(N)까지 증가할 수 있습니다.
11+
- C++에서 unordered_{set, map}은 해시 및 버킷 구조를 사용합니다. 이 경우, 평균적인 쿼리, 삽입, 삭제 비용은 O(1)입니다.
12+
우리의 요구사항은 입력에 대한 별도의 정보를 저장할 필요가 없으므로 unordered_set으로 중복 검사를 수행하는 것이 비용 효율적입니다.
13+
최악의 경우 모든 입력을 확인해야 합니다. N개의 입력에 대해 각각 쿼리와 입력을 수행하므로, 총 시간 복잡도는 O(N), 공간 복잡도도 입력만큼인 O(N)입니다.
14+
*/
15+
16+
#include <unordered_set>
17+
#include <vector>
18+
19+
class Solution {
20+
public:
21+
bool containsDuplicate(std::vector<int>& nums) {
22+
std::unordered_set<int> s;
23+
24+
for (auto it = nums.begin(); it != nums.end(); it++) {
25+
int x = *it;
26+
if (s.find(x) != s.end())
27+
return true;
28+
else
29+
s.insert(x);
30+
}
31+
return false;
32+
}
33+
};

contains-duplicate/chordpli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
class Solution:
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
dic = {}
6+
7+
for num in nums:
8+
if dic.get(num):
9+
return True
10+
dic[num] = 1
11+
12+
return False

0 commit comments

Comments
 (0)