Skip to content

Commit 5dfc797

Browse files
committed
fix: address CI issues
1 parent a77095d commit 5dfc797

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ public static int[] countDistinct(int[] arr, int k) {
3939
freqMap.merge(arr[i], 1, Integer::sum);
4040

4141
int outgoing = arr[i - k];
42-
freqMap.put(outgoing, freqMap.get(outgoing) - 1);
43-
if (freqMap.get(outgoing) == 0) {
44-
freqMap.remove(outgoing);
42+
43+
Integer count = freqMap.get(outgoing);
44+
if (count != null) {
45+
if (count == 1) {
46+
freqMap.remove(outgoing);
47+
} else {
48+
freqMap.put(outgoing, count - 1);
49+
}
4550
}
4651

4752
result[i - k + 1] = freqMap.size();

src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,26 @@ public class CountDistinctElementsInWindowTest {
99

1010
@Test
1111
public void testBasicCase() {
12-
assertArrayEquals(new int[]{3, 2, 2},
13-
CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 2, 3}, 3));
12+
assertArrayEquals(new int[] {3, 2, 2}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 2, 3}, 3));
1413
}
1514

1615
@Test
1716
public void testAllSame() {
18-
assertArrayEquals(new int[]{1, 1, 1},
19-
CountDistinctElementsInWindow.countDistinct(new int[]{2, 2, 2, 2}, 2));
17+
assertArrayEquals(new int[] {1, 1, 1}, CountDistinctElementsInWindow.countDistinct(new int[] {2, 2, 2, 2}, 2));
2018
}
2119

2220
@Test
2321
public void testAllDistinct() {
24-
assertArrayEquals(new int[]{3, 3},
25-
CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 3));
22+
assertArrayEquals(new int[] {3, 3}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 4}, 3));
2623
}
2724

2825
@Test
2926
public void testWindowSizeEqualsArray() {
30-
assertArrayEquals(new int[]{4},
31-
CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 4));
27+
assertArrayEquals(new int[] {4}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 4}, 4));
3228
}
3329

3430
@Test
3531
public void testInvalidInput() {
36-
assertThrows(IllegalArgumentException.class,
37-
() -> CountDistinctElementsInWindow.countDistinct(new int[]{}, 2));
32+
assertThrows(IllegalArgumentException.class, () -> CountDistinctElementsInWindow.countDistinct(new int[] {}, 2));
3833
}
39-
}
34+
}

0 commit comments

Comments
 (0)