From 986a34abc13df80125d5f739a3e4da2e8302b45e Mon Sep 17 00:00:00 2001 From: Cody Kim <50035753+0-Chan@users.noreply.github.com> Date: Sun, 18 Aug 2024 09:18:14 +0900 Subject: [PATCH 1/2] contains duplicate --- contains-duplicate/0-chan.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 contains-duplicate/0-chan.ts diff --git a/contains-duplicate/0-chan.ts b/contains-duplicate/0-chan.ts new file mode 100644 index 000000000..e768f9c9e --- /dev/null +++ b/contains-duplicate/0-chan.ts @@ -0,0 +1,8 @@ +/** + * time complexity : O(n) + * space complexity : O(n) + */ +function containsDuplicate(nums: number[]): boolean { + const hasDuplicate = new Set(nums).size !== nums.length; + return hasDuplicate; +}; From b589fd2d2539421c52c20a830f4f1e1209530ba2 Mon Sep 17 00:00:00 2001 From: Cody Kim <50035753+0-Chan@users.noreply.github.com> Date: Sun, 18 Aug 2024 09:22:23 +0900 Subject: [PATCH 2/2] number of 1 bits --- number-of-1-bits/0-chan.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 number-of-1-bits/0-chan.ts diff --git a/number-of-1-bits/0-chan.ts b/number-of-1-bits/0-chan.ts new file mode 100644 index 000000000..17b8fdba2 --- /dev/null +++ b/number-of-1-bits/0-chan.ts @@ -0,0 +1,10 @@ +/** + * time complexity : O(logn) + * space complexity : O(logn) + */ +function hammingWeight(n: number): number { + const MAX_NUM = 2147483648 - 1; + + const bitwiseOperated = (n & MAX_NUM).toString(2); + return bitwiseOperated.replaceAll('0', '').length; +};