Wei Ming Tang
Wei Ming Tang

Reputation: 105

Counting occurrences in array java

I want to compute the occurrences of each number in a Java array (like 1=?, 2=?, 3=?). How can my array store more than 10 value?

int [] arryNum = new int[]{4,4,4,3,4,5,4,3,4,4,4,5,4,5,5,5,4,3,2,15,4,3,4,6,4,3,4,5,4,2,4,5,4,3,2,5,4,3,5,4,0,4,3,4,5,4,3,0,4,5,4,3,5,4,2,3,2,3,4};
     int[] counter = new int[] { 0, 0, 0, 0, 0,0 };
    for (int i = 0; i < arryNum.length; i++) {
        counter[arryNum[i] ]++;
    }

    for (int i = 0; i < counter.length; i++){
        System.out.println((i + 1) + ":" + counter[i]);
    }

Upvotes: 1

Views: 3492

Answers (5)

package com.report.automation;

import java.util.HashMap; import java.util.Map;

public class Frequency { public static void main(String[] args){

    String value[] = {"Mukesh","Mukesh","Sasi","Senthil","Mukesh","Mukesh"};
    String match = "Mukesh";
     int count = 0;
    for (int j = 0; j <= 5; j++) {
    if (match.equals(value[j])) {       
       
        count++;                
        
    }       
    }
    System.out.println(count);
    
}
}

Upvotes: 0

K Shaikh
K Shaikh

Reputation: 401

Considering the array contains String values, put the String into HashMap. Before inserting each String, check if a key exists for it in the map already If yes - take the default value for that key, which is 1, and increment it by 1 and insert again. If no - simply add the new key with default value 1 Final map will give "String":Number of occurences of string.

public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map = new HashMap();
  for(String s : strings){
    if(map.containsKey(s)){
      map.put(s, map.get(s)+1);
    }
    else
    map.put(s, 1);
  }
  return map;
}

Upvotes: 0

GhostCat
GhostCat

Reputation: 140573

You can do it like this.

First you need that Map to count your things:

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

Then you iterate your numbers; best using for-each:

for (int number : arryNum) {
  if (countsByNumbers.containsKey(number)) {
    int newCount = countsByNumbers.get(number) +1;
    countsByNumbers.put(number, newCount);
  } else {
    countsByNumbers.put(number, 1);
  }

Some notes:

  1. Be aware that Maps, as any kind of Java Collection class only deals with reference types; therefore it is declared to use Integer, and not the primitive type int!
  2. The compiler does some magic to turn the int values from your array into Integer objects under the cover
  3. Note that Map is an interface, but that we have to instantiate a concrete class, in this case we simply use HashMap

Upvotes: 2

user6904265
user6904265

Reputation: 1938

You could write it in a more succint way with java 8 streams:

Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Arrays.stream(arryNum).forEach(x -> map.put(x , map.computeIfAbsent(x, s -> 0) + 1));
System.out.println(map);

Upvotes: 3

Saurabh Khare
Saurabh Khare

Reputation: 1277

public static void main(String[] args){
    int[] arryNum = new int[] { 4, 4, 4, 3, 4, 5, 4, 3, 4, 4, 4, 5, 4, 5, 5, 5, 4, 3, 2, 15, 4,
            3, 4, 6, 4, 3, 4, 5, 4, 2, 4, 5, 4, 3, 2, 5, 4, 3, 5, 4, 0, 4, 3, 4, 5, 4, 3, 0, 4,
            5, 4, 3, 5, 4, 2, 3, 2, 3, 4 };
    Map<Integer, Integer> lookup = new HashMap<>();
    for (int key : arryNum) {
        if(lookup.containsKey(key)) {
            lookup.put(key, lookup.get(key) + 1);
        } else {
            lookup.put(key, 1);
        }
    }

    for (Integer keys : lookup.keySet()) {
        System.out.println(keys + " Found " + lookup.get(keys) + " Times");
    }

}

Upvotes: 1

Related Questions