Sunday, January 23, 2022

frequency of integer

 package arrayoperations;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class MainApp {

public static void main(String[] args) {

int[] a = { 1, 2, 2, 3, 4, 4, 4 };

Map<Integer, Integer> frequency = new HashMap();

for (int i = 0; i < a.length; i++) {

if (frequency.containsKey(a[i])) {

frequency.put(a[i], frequency.get(a[i]) + 1);

} else {

frequency.put(a[i], 1);

}

}

Set<Integer> keySet = frequency.keySet();

for(Integer i: keySet) {

System.out.println("item: "+ i+ " frequency: "+frequency.get(i));

}

}

}

No comments:

Post a Comment

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...