Java program to count each occurrence of each digit in an array

<code>

/*Java program to count each occurance of each digit in an array */

import java.util.Map;

import java.util.HashMap;

public class HelloWorld {

public static void main(String[] args) {

int a[] = {1, 2, 9, 3, 6, 4, 5, 5, 1, 3, 5, 8, 1, 4};

HashMap < Integer, Integer > intCounts = new HashMap < Integer, Integer > ();

for (int b: a) {

Integer i = intCounts.get(b);

if (i == null)

intCounts.put(b, 1);

else

intCounts.put(b, i + 1);

}

System.out.println(intCounts);

}

}

</code>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s