File tree 4 files changed +52
-6
lines changed
solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct
4 files changed +52
-6
lines changed Original file line number Diff line number Diff line change @@ -122,10 +122,9 @@ class Solution {
122
122
public int minimumOperations (int [] nums ) {
123
123
Set<Integer > s = new HashSet<> ();
124
124
for (int i = nums. length - 1 ; i >= 0 ; -- i) {
125
- if (s . contains (nums[i])) {
125
+ if (! s . add (nums[i])) {
126
126
return i / 3 + 1 ;
127
127
}
128
- s. add(nums[i]);
129
128
}
130
129
return 0 ;
131
130
}
@@ -180,6 +179,24 @@ function minimumOperations(nums: number[]): number {
180
179
}
181
180
```
182
181
182
+ #### Rust
183
+
184
+ ``` rust
185
+ use std :: collections :: HashSet ;
186
+
187
+ impl Solution {
188
+ pub fn minimum_operations (nums : Vec <i32 >) -> i32 {
189
+ let mut s = HashSet :: new ();
190
+ for i in (0 .. nums . len ()). rev () {
191
+ if ! s . insert (nums [i ]) {
192
+ return (i / 3 ) as i32 + 1 ;
193
+ }
194
+ }
195
+ 0
196
+ }
197
+ }
198
+ ```
199
+
183
200
<!-- tabs: end -->
184
201
185
202
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -118,10 +118,9 @@ class Solution {
118
118
public int minimumOperations (int [] nums ) {
119
119
Set<Integer > s = new HashSet<> ();
120
120
for (int i = nums. length - 1 ; i >= 0 ; -- i) {
121
- if (s . contains (nums[i])) {
121
+ if (! s . add (nums[i])) {
122
122
return i / 3 + 1 ;
123
123
}
124
- s. add(nums[i]);
125
124
}
126
125
return 0 ;
127
126
}
@@ -176,6 +175,24 @@ function minimumOperations(nums: number[]): number {
176
175
}
177
176
```
178
177
178
+ #### Rust
179
+
180
+ ``` rust
181
+ use std :: collections :: HashSet ;
182
+
183
+ impl Solution {
184
+ pub fn minimum_operations (nums : Vec <i32 >) -> i32 {
185
+ let mut s = HashSet :: new ();
186
+ for i in (0 .. nums . len ()). rev () {
187
+ if ! s . insert (nums [i ]) {
188
+ return (i / 3 ) as i32 + 1 ;
189
+ }
190
+ }
191
+ 0
192
+ }
193
+ }
194
+ ```
195
+
179
196
<!-- tabs: end -->
180
197
181
198
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -2,10 +2,9 @@ class Solution {
2
2
public int minimumOperations (int [] nums ) {
3
3
Set <Integer > s = new HashSet <>();
4
4
for (int i = nums .length - 1 ; i >= 0 ; --i ) {
5
- if (s . contains (nums [i ])) {
5
+ if (! s . add (nums [i ])) {
6
6
return i / 3 + 1 ;
7
7
}
8
- s .add (nums [i ]);
9
8
}
10
9
return 0 ;
11
10
}
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ impl Solution {
4
+ pub fn minimum_operations ( nums : Vec < i32 > ) -> i32 {
5
+ let mut s = HashSet :: new ( ) ;
6
+ for i in ( 0 ..nums. len ( ) ) . rev ( ) {
7
+ if !s. insert ( nums[ i] ) {
8
+ return ( i / 3 ) as i32 + 1 ;
9
+ }
10
+ }
11
+ 0
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments