Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 <a href="https://www.geeksforgeeks.org/count-distinct-elements-in-every-window-of-size-k/">Reference</a>
*/
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<Integer, Integer> 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];

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();
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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}, 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));
}
}
Loading