Skip to content

Commit 6735f7a

Browse files
authored
Create Minimum Number of Operations to Make Elements in Array Distinct.java
1 parent 199a76c commit 6735f7a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int minimumOperations(int[] nums) {
3+
Set<Integer> set = new HashSet<>();
4+
int index = -1;
5+
for (int i = nums.length - 1; i >= 0; i--) {
6+
if (set.contains(nums[i])) {
7+
index = i;
8+
break;
9+
}
10+
set.add(nums[i]);
11+
}
12+
if (index == -1) {
13+
return 0;
14+
}
15+
index++;
16+
return index % 3 == 0 ? index / 3 : (index / 3) + 1;
17+
}
18+
}

0 commit comments

Comments
 (0)