How to print the individual occurance of elements in Java?

+4 votes
I want to print the individual occurence of every element in array. How can i do it?

eg:

{1,1,1,2,2,2,2,3,3,3,3,3,1,1,1,2}

O/P : 1: 6 , 2: 5 , 3: 5

Thank you in advance.
Dec 4, 2018 in Java by Kanishk
• 880 points
128 views

1 answer to this question.

+2 votes
Best answer

You can use a HashMap to serve the purpose.

In the HashMap you can store the elements and the count of it.

Here is a simple implementation I used.

import java.util.HashMap;
import java.util.Scanner;
public class CountElement 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of elements in the Array");
        int n = scan.nextInt();
        int arr[] = new int [n];
        System.out.println("Enter "+n+" elements");
        for(int i=0; i<n; i++)
        {
            arr[i] = scan.nextInt();
        }
        HashMap<Integer,Integer> countMap = new HashMap<Integer, Integer>();
        for (int i : arr)
        {
            if (countMap.containsKey(i))
            {
                countMap.put(i, countMap.get(i)+1);
            }
            else
            {
                countMap.put(i, 1);
            }
        }
        System.out.println(countMap);
    }
}
Output:
Enter the number of elements in the Array
7
Enter 7 elements
1 2 1 1 2 3 4 
{1=3, 2=2, 3=1, 4=1}

Hope this will suffice your work.

answered Dec 4, 2018 by Priyaj
• 57,300 points

selected Dec 4, 2018 by Omkar

Related Questions In Java

0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
99 views
0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,050 points
108 views
+1 vote
1 answer

How to tell the Maven to use the latest version of dependency in Java?

If you always want to use the ...READ MORE

answered Jun 26, 2018 in Java by Sushmita
• 6,880 points
2,383 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,880 points
167 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
81 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
76 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
728 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
65 views
0 votes
1 answer

How to find the nth prime number in Java?

Here is the code to find nth ...READ MORE

answered Mar 4 in Java by Priyaj
• 57,300 points
10,600 views
+15 votes
18 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7 in Java by Suresh
• 700 points
96,015 views