code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Distinctelementsinwindow

import java.util.HashMap;

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class DistinctElementsInWindow {

  static void printDistinct(int nums[], int k) {
    HashMap<Integer, Integer> hm = new HashMap<>();
    for(int i = 0; i < k ; i++){
      if(hm.containsKey(nums[i])){
        hm.put(nums[i], hm.get(nums[i]) + 1);
      } else {
        hm.put(nums[i], 1);
      }
    }
    System.out.println("Distinct Elements - " + hm.size());
    for(int i = k; i < nums.length; i++){
      if(hm.containsKey(nums[i-k])){
        hm.put(nums[i-k], hm.get(nums[i-k]) - 1);
      }
      if(hm.get(nums[i-k]) == 0){
        hm.remove(nums[i-k]);
      }
      if(hm.containsKey(nums[i])){
        hm.put(nums[i], hm.get(nums[i]) + 1);
      } else {
        hm.put(nums[i], 0);
      }
      System.out.println("Distinct Elements - " + hm.size());
    }
  }

  public static void main(String[] args){
    int arr[] = new int[]{10, 10, 5, 3, 20, 5};
    int k=4;
    printDistinct(arr, k);
  }
}

Created 2021-12-28T19:51:08+00:00, updated 2021-12-29T05:57:04+00:00 · History · Edit