-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveElement027.java
63 lines (56 loc) · 1.98 KB
/
RemoveElement027.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
class BadSolution {
public int removeElement(int[] nums, int val) {
HashMap<Integer, Integer> valueMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length - 1; i++) {
valueMap.put(nums[i], (int) valueMap.getOrDefault(nums[i], 0) + 1);
nums[i] = 0;
//put all da values in da hashmap
}
int k = nums.length - 1 - Integer.parseInt(valueMap.get(val).toString());
valueMap.remove(val);
Set<Integer> keySet = valueMap.keySet();
//uhhhh
List<Integer> result = keySet.stream()
.flatMap(key -> {
int value = valueMap.get(key);
return Arrays.stream(new Integer[value]).map(v -> value);
}).collect(Collectors.toList());
for (int i = 0; i < result.size(); i++) {
nums[i] = Integer.valueOf(result.get(i).toString());
}
return k;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
int end = nums.length - 1;
for (int i = 0; i <= end; i++) {
if (nums[i] == val) {
swapRemove(nums, i, end);
end--;
i--; // re-check the current index after the swap
}
}
return end + 1;
}
private void swapRemove(int[] nums, int index, int end) {
nums[index] = nums[end];
nums[end] = -1; // Optional: Mark end element as removed (for clarity)
}
}
public class RemoveElement027 {
public static void main(String[] args) {
Solution solution = new Solution();
int[] test1 = {0,1,2,2,3,0,4,2};
int[] test2 = {3,2,2,3};
solution.removeElement(test1, 2);
System.out.println(Arrays.toString(test1));
solution.removeElement(test2, 3);
System.out.println(Arrays.toString(test2));
}
}