From 70f1cd72265df47ba5dba6ceb683dfc5f4da918a Mon Sep 17 00:00:00 2001 From: Abdullah Shah Date: Wed, 10 Jun 2026 17:16:59 +0500 Subject: [PATCH 1/3] feat: add count distinct elements in window algorithm --- .../CountDistinctElementsInWindow.java | 52 +++++++++++++++++++ .../CountDistinctElementsInWindowTest.java | 39 ++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java create mode 100644 src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java b/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java new file mode 100644 index 000000000000..945431a377b3 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java @@ -0,0 +1,52 @@ +package com.thealgorithms.slidingwindow; + +import java.util.HashMap; +import java.util.Map; + +/** + * Counts the number of distinct elements in every window of size k. + * + * @see Reference + */ +public final class CountDistinctElementsInWindow { + + private CountDistinctElementsInWindow() { + } + + /** + * Returns an array where each element is the count of distinct + * elements in the corresponding window of size k. + * + * @param arr the input array + * @param k the window size + * @return array of distinct element counts per window + */ + public static int[] countDistinct(int[] arr, int k) { + if (arr == null || arr.length == 0 || k <= 0 || k > arr.length) { + throw new IllegalArgumentException("Invalid input"); + } + + int n = arr.length; + int[] result = new int[n - k + 1]; + Map freqMap = new HashMap<>(); + + for (int i = 0; i < k; i++) { + freqMap.merge(arr[i], 1, Integer::sum); + } + result[0] = freqMap.size(); + + for (int i = k; i < n; i++) { + freqMap.merge(arr[i], 1, Integer::sum); + + int outgoing = arr[i - k]; + freqMap.put(outgoing, freqMap.get(outgoing) - 1); + if (freqMap.get(outgoing) == 0) { + freqMap.remove(outgoing); + } + + result[i - k + 1] = freqMap.size(); + } + + return result; + } +} diff --git a/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java new file mode 100644 index 000000000000..08613894c934 --- /dev/null +++ b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java @@ -0,0 +1,39 @@ +package com.thealgorithms.slidingwindow; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class CountDistinctElementsInWindowTest { + + @Test + public void testBasicCase() { + assertArrayEquals(new int[]{3, 2, 2, 2}, + CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 2, 3}, 3)); + } + + @Test + public void testAllSame() { + assertArrayEquals(new int[]{1, 1, 1}, + CountDistinctElementsInWindow.countDistinct(new int[]{2, 2, 2, 2}, 2)); + } + + @Test + public void testAllDistinct() { + assertArrayEquals(new int[]{3, 3}, + CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 3)); + } + + @Test + public void testWindowSizeEqualsArray() { + assertArrayEquals(new int[]{4}, + CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 4)); + } + + @Test + public void testInvalidInput() { + assertThrows(IllegalArgumentException.class, + () -> CountDistinctElementsInWindow.countDistinct(new int[]{}, 2)); + } +} \ No newline at end of file From a77095d120dac0f434147176798cc4d63c6e350f Mon Sep 17 00:00:00 2001 From: Abdullah Shah Date: Wed, 10 Jun 2026 17:40:59 +0500 Subject: [PATCH 2/3] feat: add CountDistinctElementsInWindow --- .../slidingwindow/CountDistinctElementsInWindowTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java index 08613894c934..a3c5ff0cc14e 100644 --- a/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java +++ b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java @@ -9,7 +9,7 @@ public class CountDistinctElementsInWindowTest { @Test public void testBasicCase() { - assertArrayEquals(new int[]{3, 2, 2, 2}, + assertArrayEquals(new int[]{3, 2, 2}, CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 2, 3}, 3)); } From 5dfc797650782ca610af51b3409fdf2e0d45ca84 Mon Sep 17 00:00:00 2001 From: Abdullah Shah Date: Wed, 10 Jun 2026 18:19:33 +0500 Subject: [PATCH 3/3] fix: address CI issues --- .../CountDistinctElementsInWindow.java | 11 ++++++++--- .../CountDistinctElementsInWindowTest.java | 17 ++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java b/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java index 945431a377b3..19e573437f6d 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java +++ b/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java @@ -39,9 +39,14 @@ public static int[] countDistinct(int[] arr, int k) { freqMap.merge(arr[i], 1, Integer::sum); int outgoing = arr[i - k]; - freqMap.put(outgoing, freqMap.get(outgoing) - 1); - if (freqMap.get(outgoing) == 0) { - freqMap.remove(outgoing); + + Integer count = freqMap.get(outgoing); + if (count != null) { + if (count == 1) { + freqMap.remove(outgoing); + } else { + freqMap.put(outgoing, count - 1); + } } result[i - k + 1] = freqMap.size(); diff --git a/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java index a3c5ff0cc14e..a6931bca99d2 100644 --- a/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java +++ b/src/test/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindowTest.java @@ -9,31 +9,26 @@ public class CountDistinctElementsInWindowTest { @Test public void testBasicCase() { - assertArrayEquals(new int[]{3, 2, 2}, - CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 2, 3}, 3)); + assertArrayEquals(new int[] {3, 2, 2}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 2, 3}, 3)); } @Test public void testAllSame() { - assertArrayEquals(new int[]{1, 1, 1}, - CountDistinctElementsInWindow.countDistinct(new int[]{2, 2, 2, 2}, 2)); + assertArrayEquals(new int[] {1, 1, 1}, CountDistinctElementsInWindow.countDistinct(new int[] {2, 2, 2, 2}, 2)); } @Test public void testAllDistinct() { - assertArrayEquals(new int[]{3, 3}, - CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 3)); + assertArrayEquals(new int[] {3, 3}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 4}, 3)); } @Test public void testWindowSizeEqualsArray() { - assertArrayEquals(new int[]{4}, - CountDistinctElementsInWindow.countDistinct(new int[]{1, 2, 3, 4}, 4)); + assertArrayEquals(new int[] {4}, CountDistinctElementsInWindow.countDistinct(new int[] {1, 2, 3, 4}, 4)); } @Test public void testInvalidInput() { - assertThrows(IllegalArgumentException.class, - () -> CountDistinctElementsInWindow.countDistinct(new int[]{}, 2)); + assertThrows(IllegalArgumentException.class, () -> CountDistinctElementsInWindow.countDistinct(new int[] {}, 2)); } -} \ No newline at end of file +}