File tree Expand file tree Collapse file tree 4 files changed +52
-6
lines changed
solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct Expand file tree Collapse file tree 4 files changed +52
-6
lines changed Original file line number Diff line number Diff line change @@ -122,10 +122,9 @@ class Solution {
122122 public int minimumOperations (int [] nums ) {
123123 Set<Integer > s = new HashSet<> ();
124124 for (int i = nums. length - 1 ; i >= 0 ; -- i) {
125- if (s . contains (nums[i])) {
125+ if (! s . add (nums[i])) {
126126 return i / 3 + 1 ;
127127 }
128- s. add(nums[i]);
129128 }
130129 return 0 ;
131130 }
@@ -180,6 +179,24 @@ function minimumOperations(nums: number[]): number {
180179}
181180```
182181
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+
183200<!-- tabs: end -->
184201
185202<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -118,10 +118,9 @@ class Solution {
118118 public int minimumOperations (int [] nums ) {
119119 Set<Integer > s = new HashSet<> ();
120120 for (int i = nums. length - 1 ; i >= 0 ; -- i) {
121- if (s . contains (nums[i])) {
121+ if (! s . add (nums[i])) {
122122 return i / 3 + 1 ;
123123 }
124- s. add(nums[i]);
125124 }
126125 return 0 ;
127126 }
@@ -176,6 +175,24 @@ function minimumOperations(nums: number[]): number {
176175}
177176```
178177
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+
179196<!-- tabs: end -->
180197
181198<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -2,10 +2,9 @@ class Solution {
22 public int minimumOperations (int [] nums ) {
33 Set <Integer > s = new HashSet <>();
44 for (int i = nums .length - 1 ; i >= 0 ; --i ) {
5- if (s . contains (nums [i ])) {
5+ if (! s . add (nums [i ])) {
66 return i / 3 + 1 ;
77 }
8- s .add (nums [i ]);
98 }
109 return 0 ;
1110 }
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