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
• 890 points
1,224 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
• 58,020 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
838 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,140 points
1,646 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,920 points
15,375 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,920 points
13,188 views
+1 vote
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,620 points
1,324 views
+1 vote
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,290 points
1,185 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,220 points
3,575 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,220 points
992 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, 2019 in Java by Priyaj
• 58,020 points
39,338 views
+17 votes
25 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, 2019 in Java by Suresh
• 720 points
253,584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP