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

Countfrequency

import java.util.HashMap;

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

class CountFrequency {

  static void func(int[] nums) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < nums.length; i++) {
      if (map.containsKey(nums[i])) {
        map.put(nums[i], map.get(nums[i]) + 1);
      } else {
        map.put(nums[i], 1);
      }
    }
    for (HashMap.Entry<Integer, Integer> itr : map.entrySet()) {
      System.out.println(itr.getKey() + " " + itr.getValue());
    }
  }

  public static void main(String[] args) {
    int arr[] = new int[] { 15, 16, 27, 27, 28, 15 };
    func(arr);
  }
}

Created 2021-12-26T20:02:15+00:00 · Edit