Count occurance of string in an array

0 votes

Please help me find a way to solve this efficiently.

Problem statement: Find individual occurance of every character in an array.

Input:

[a,a,a,b,b,b,c,c,c,c,a]

Output:

a: 4
b: 3
c: 4
Mar 6 in Java by Bobin
11 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use hashmap to solve this, here is the function for implementing it,

Map<String,Integer> hm = new HashMap();
for(String x:array){
    if(!hm.containsKey(x)){
        hm.put(x,1);
    }else{
        hm.put(x, hm.get(x)+1);
    }
}
System.out.println(hm);
answered Mar 7 by Priyaj
• 56,120 points

Related Questions In Java

0 votes
1 answer

How to convert an int array to string using tostring method in java?

you want to use the Arrays.toString(int[]) method: import java.util.Arrays; int[] array ...READ MORE

answered Jun 7, 2018 in Java by Akrati
• 3,150 points
110 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,820 points
54 views
0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,310 points
35 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
81 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,520 points
34 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
22 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,320 points
201 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
99 views
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 56,120 points
89 views
+14 votes
17 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
52,180 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.