@@ -161,25 +161,6 @@ func containsDuplicate(nums []int) bool {
161
161
}
162
162
```
163
163
164
- ### ** TypeScript**
165
-
166
- ``` ts
167
- function containsDuplicate(nums : number []): boolean {
168
- let unique: Set <number > = new Set (nums );
169
- return unique .size != nums .length ;
170
- }
171
- ```
172
-
173
- ### ** C#**
174
-
175
- ``` cs
176
- public class Solution {
177
- public bool ContainsDuplicate (int [] nums ) {
178
- return nums .Distinct ().Count () < nums .Length ;
179
- }
180
- }
181
- ```
182
-
183
164
### ** JavaScript**
184
165
185
166
``` js
@@ -192,17 +173,29 @@ var containsDuplicate = function (nums) {
192
173
};
193
174
```
194
175
195
- ### ** Rust **
176
+ ### ** TypeScript **
196
177
197
- ``` rust
198
- use std :: collections :: HashSet ;
199
- impl Solution {
200
- pub fn contains_duplicate (nums : Vec <i32 >) -> bool {
201
- nums . iter (). collect :: <HashSet <& i32 >>(). len () != nums . len ()
178
+ ``` ts
179
+ function containsDuplicate(nums : number []): boolean {
180
+ nums .sort ((a , b ) => a - b );
181
+ const n = nums .length ;
182
+ for (let i = 1 ; i < n ; i ++ ) {
183
+ if (nums [i - 1 ] === nums [i ]) {
184
+ return true ;
185
+ }
202
186
}
187
+ return false ;
203
188
}
204
189
```
205
190
191
+ ``` ts
192
+ function containsDuplicate(nums : number []): boolean {
193
+ return new Set <number >(nums ).size !== nums .length ;
194
+ }
195
+ ```
196
+
197
+ ### ** Rust**
198
+
206
199
``` rust
207
200
impl Solution {
208
201
pub fn contains_duplicate (mut nums : Vec <i32 >) -> bool {
@@ -218,6 +211,43 @@ impl Solution {
218
211
}
219
212
```
220
213
214
+ ``` rust
215
+ use std :: collections :: HashSet ;
216
+ impl Solution {
217
+ pub fn contains_duplicate (nums : Vec <i32 >) -> bool {
218
+ nums . iter (). collect :: <HashSet <& i32 >>(). len () != nums . len ()
219
+ }
220
+ }
221
+ ```
222
+
223
+ ### ** C**
224
+
225
+ ``` c
226
+ int cmp (const void * a, const void * b) {
227
+ return * (int * ) a - * (int * ) b;
228
+ }
229
+
230
+ bool containsDuplicate(int * nums, int numsSize) {
231
+ qsort(nums, numsSize, sizeof(int), cmp);
232
+ for (int i = 1; i < numsSize; i++) {
233
+ if (nums[ i - 1] == nums[ i] ) {
234
+ return 1;
235
+ }
236
+ }
237
+ return 0;
238
+ }
239
+ ```
240
+
241
+ ### **C#**
242
+
243
+ ```cs
244
+ public class Solution {
245
+ public bool ContainsDuplicate(int[] nums) {
246
+ return nums.Distinct().Count() < nums.Length;
247
+ }
248
+ }
249
+ ```
250
+
221
251
### ** ...**
222
252
223
253
```
0 commit comments