Skip to content

Commit 5dd5a53

Browse files
committed
leetcode
1 parent 8cd97a5 commit 5dd5a53

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
-* Contains Duplicate *-
4+
5+
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.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [1,2,3,1]
12+
Output: true
13+
Example 2:
14+
15+
Input: nums = [1,2,3,4]
16+
Output: false
17+
Example 3:
18+
19+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
20+
Output: true
21+
22+
23+
Constraints:
24+
25+
1 <= nums.length <= 105
26+
-109 <= nums[i] <= 109
27+
28+
29+
*/
30+
31+
class A {
32+
// Brute Force
33+
// Runtime: 2349 ms, faster than 10.54% of Dart online submissions for Contains Duplicate.
34+
// Memory Usage: 173.3 MB, less than 23.00% of Dart online submissions for Contains Duplicate.
35+
bool containsDuplicate(List<int> nums) {
36+
if (nums.length == 0) return true;
37+
for (int i = 0; i < nums.length; i++) {
38+
for (int j = i + 1; j < nums.length; j++) {
39+
if (nums[i] == nums[j]) {
40+
return true;
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
}
47+
48+
class B {
49+
// O(nlogn)
50+
// Runtime: 716 ms, faster than 23.32% of Dart online submissions for Contains Duplicate.
51+
// Memory Usage: 167.4 MB, less than 81.79% of Dart online submissions for Contains Duplicate.
52+
bool containsDuplicate(List<int> nums) {
53+
nums.sort();
54+
for (int i = 1; i < nums.length; i++) {
55+
if (nums[i - 1] == nums[i] || nums.length == 0) {
56+
return true;
57+
}
58+
}
59+
return false;
60+
}
61+
}
62+
63+
class C {
64+
// Using SET O(n)
65+
// Runtime: 794 ms, faster than 16.93% of Dart online submissions for Contains Duplicate.
66+
// Memory Usage: 172.5 MB, less than 30.99% of Dart online submissions for Contains Duplicate.
67+
bool containsDuplicate(List<int> nums) {
68+
if (nums.length == 0) return true;
69+
Set s = Set();
70+
for (var i = 0; i < nums.length; i++) {
71+
if (s.contains(nums[i])) {
72+
return true;
73+
}
74+
s.add(nums[i]);
75+
}
76+
return false;
77+
}
78+
}
79+
80+
class D {
81+
bool containsDuplicate(List<int> nums) {
82+
Set<int> hs = Set(); //taken a HashSet
83+
for (int x in nums) {
84+
hs.add(x);
85+
} //added all the array elements in HashSet and HashSet will remove the Delicacy
86+
return hs.length !=
87+
nums.length; // if HashSet Size is not equal to array Length it means there were duplicate elements and if HashSet size and Array length is same then no Duplicates present
88+
}
89+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func containsDuplicate(nums []int) bool {
4+
if len(nums) == 0 {
5+
return true
6+
}
7+
for i := 0; i < len(nums); i++ {
8+
for j := i + 1; j < len(nums); j++ {
9+
if nums[i] == nums[j] {
10+
return true
11+
}
12+
}
13+
}
14+
return false
15+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 🔥 Contains Duplicate 🔥 || 3 Solution || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Brute Force
4+
5+
```dart
6+
class Solution {
7+
// Runtime: 2349 ms, faster than 10.54% of Dart online submissions for Contains Duplicate.
8+
// Memory Usage: 173.3 MB, less than 23.00% of Dart online submissions for Contains Duplicate.
9+
bool containsDuplicate(List<int> nums) {
10+
// if the length is 0
11+
if (nums.length == 0) return true;
12+
// looping through each element in the array
13+
for (int i = 0; i < nums.length; i++) {
14+
// inner loop to iterate through the next value
15+
for (int j = i + 1; j < nums.length; j++) {
16+
// if they are same than true
17+
if (nums[i] == nums[j]) {
18+
return true;
19+
}
20+
}
21+
}
22+
// else false
23+
return false;
24+
}
25+
}
26+
```
27+
28+
## Solution - 2 O(nlogn)
29+
30+
```dart
31+
class Solution {
32+
// Runtime: 716 ms, faster than 23.32% of Dart online submissions for Contains Duplicate.
33+
// Memory Usage: 167.4 MB, less than 81.79% of Dart online submissions for Contains Duplicate.
34+
bool containsDuplicate(List<int> nums) {
35+
// sorting to arrange every element in a list
36+
nums.sort();
37+
// looping through each and every element
38+
for (int i = 1; i < nums.length; i++) {
39+
// if i - 1 is index so index is same as value than true
40+
if (nums[i - 1] == nums[i] || nums.length == 0) {
41+
return true;
42+
}
43+
}
44+
// else false
45+
return false;
46+
}
47+
}
48+
```
49+
50+
## Solution - 3 SET O(n)
51+
52+
```dart
53+
class Solution {
54+
// Runtime: 794 ms, faster than 16.93% of Dart online submissions for Contains Duplicate.
55+
// Memory Usage: 172.5 MB, less than 30.99% of Dart online submissions for Contains Duplicate.
56+
bool containsDuplicate(List<int> nums) {
57+
// if it empty
58+
if (nums.length == 0) return true;
59+
// set to store our value
60+
Set s = Set();
61+
// looping through each element
62+
for (int i = 0; i < nums.length; i++) {
63+
// set have pairs value so if both of the are same than it' true
64+
if (s.contains(nums[i])) {
65+
return true;
66+
}
67+
// adding the list value inside the set
68+
s.add(nums[i]);
69+
}
70+
// if not found return false
71+
return false;
72+
}
73+
}
74+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7676
- [Time Based Key-Value Store](TimeBasedKeyValueStore/time_based_key_value_store.dart)
7777
- [My Calendar III](MyCalendar-III/my_calendar_III.dart)
7878
- [Reverse Linked List](ReverseLinkedList/reverse_linked_list.dart)
79+
- [Contains Duplicate](ContainsDuplicate/contains_duplicate.dart)
7980

8081
## Reach me via
8182

0 commit comments

Comments
 (0)