Skip to content

Commit 5445b3e

Browse files
Problem 217 (#22)
* add solution and resources * update readme
1 parent 7c81db0 commit 5445b3e

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
2929
21. [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
3030
22. [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
3131
23. [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
32-
24. Contains Duplicate #217
32+
24. [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
3333

3434
### Medium
3535

@@ -100,6 +100,7 @@ In order to practice with similar data structures I'll be placing each problem i
100100
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
101101
- [Maximum Subarray #53](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/maximum-subarray-53.md)
102102
- [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
103+
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
103104

104105
### Queue
105106

@@ -134,6 +135,7 @@ In order to practice with similar data structures I'll be placing each problem i
134135
- [Climbing Stairs #70](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/climbing-stairs-70.md)
135136
- [Longest Palindrome #409](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/longest-palindrome-409.md)
136137
- [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
138+
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
137139

138140
### Binary Tree
139141

@@ -154,6 +156,7 @@ Within the problems above there are several patterns that often occur. I plan to
154156
- [Linked List Cycle #141](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/linked-list-cycle-141.md)
155157
- [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
156158
- [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
159+
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
157160

158161
### Binary Search
159162

easy/contains-duplicate-217.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Contains Duplicate
2+
3+
Page on leetcode: https://leetcode.com/problems/contains-duplicate/
4+
5+
## Problem Statement
6+
7+
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
8+
9+
### Constraints
10+
11+
- 1 <= nums.length <= 105
12+
- -109 <= nums[i] <= 109
13+
14+
### Example
15+
16+
```
17+
Input: nums = [1,2,3,1]
18+
Output: true
19+
```
20+
21+
```
22+
Input: nums = [1,2,3,4]
23+
Output: false
24+
```
25+
26+
## Solution
27+
28+
Immediate thought is to use a hashmap and count the numbers as you iterate thru the array.
29+
30+
### Pseudocode
31+
32+
1. Create empty map
33+
2. For loop thru array
34+
3. If element exist in map return true
35+
4. else add element to map with val 1
36+
5. return false if it loops thru entire array
37+
38+
### Initial Solution
39+
40+
This solution has a time and space complexity of O(n).
41+
42+
```javascript
43+
const containsDuplicate = function (nums) {
44+
const map = {};
45+
46+
for (let i = 0; i < nums.length; i++) {
47+
if (nums[i] in map) {
48+
return true;
49+
}
50+
map[nums[i]] = 1;
51+
}
52+
53+
return false;
54+
};
55+
```
56+
57+
### Alternative Solution
58+
59+
The below alternative has a time complexity O(nlogn) due to sorting, but has a space complexity of O(1). You can see explanations of different solutions here: https://www.youtube.com/watch?v=3OamzN90kPg
60+
61+
```javascript
62+
const containsDuplicate = function (nums) {
63+
nums.sort((a, b) => a - b);
64+
65+
let left = 0;
66+
let right = 1;
67+
while (nums[right] !== undefined) {
68+
if (nums[left] === nums[right]) {
69+
return true;
70+
}
71+
left++;
72+
right++;
73+
}
74+
75+
return false;
76+
};
77+
```
File renamed without changes.

0 commit comments

Comments
 (0)