0

I have an Arraylist as follows , I need to count the number of occurrences for all of the elements in the Arraylist, knowing that the content of the Arraylist may change according to the user input. i.e : the user in the second time may enter(C and D)

ArrayList<String> letters = new ArrayList<String>();

letters.add("A");
letters.add("B");
letters.add("A");
letters.add("B");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Nermeen
  • 11
  • 1
  • Do you mean, you want to count how many time an element is present in the list? – rohitmohta May 13 '16 at 04:41
  • 2
    @Nermeen just to avoid extra coding, why don't you just use `HashMap`, and maintain count, as simple as that. – Ankur Singhal May 13 '16 at 04:44
  • Yes I need to find how many A's and B's exist – Nermeen May 13 '16 at 04:47
  • Other than the naming of your variable as `letters`, nothing in your question text restricts the strings in the list to be just single-letter strings. Is that a restriction, or can the strings be words or entire sentences? If restricted, can they only be uppercase letters, and is lowercase allowed? Digits? Special characters? International letters like `ä`? – Andreas May 13 '16 at 04:53
  • @Nermeen Please take more care when posting. Your example code was improperly formatted, and contained much white space with more mysterious unprintable characters than printable characters. – Basil Bourque May 13 '16 at 05:13
  • @Nermeen Please search Stack Overflow thoroughly before posting. Also a duplicate of [this](http://stackoverflow.com/q/14260134/642706), [this](http://stackoverflow.com/q/2647232/642706), [this](http://stackoverflow.com/q/7687062/642706), and more – Basil Bourque May 13 '16 at 05:20

2 Answers2

4

You can use a Map to keep track of the tokens (and counts). If you use a LinkedHashMap it will preserve insertion order. I would use a for-each loop to iterate the letters; if the map doesn't currently contain the letter set it to 1 (otherwise increment the current count). Finally, print the map. Something like

Map<String, Integer> map = new LinkedHashMap<>();
for (String letter : letters) {
    map.put(letter, !map.containsKey(letter) ? 1 : 1 + map.get(letter));
}
System.out.println(map);

Also, you can initialize your List when you declare it like

List<String> letters = Arrays.asList("A", "B", "A", "B");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
-1

Iterate over the ArrayList and check if each element matches a particular String:

String test = //string you want to count occurrences of
int count = 0;
for (String s : letters) {
    if (s.equals(test)) {
        count++;
    }
}
Bethany Louise
  • 646
  • 7
  • 13
  • I think they want the count for every element. So, a HashMap will be required to count occurence for every unique element within the list. – rohitmohta May 13 '16 at 04:44
  • What is `letters`? Nothing in the question says that strings in the list are only single letters. That's just an example. – Andreas May 13 '16 at 04:48
  • @Andreas `letters` is the `ArrayList`. It's right there in OP's code. – Bethany Louise May 13 '16 at 04:58
  • Oh sorry, you are right. Then, what is *"string you want to count occurrences of"*, when the question explicitly states that you don't know what to count since *"the Arraylist may change according to the user input"*? – Andreas May 13 '16 at 05:01
  • @Andreas It could be inputted by the user or something, depending on what OP wants. – Bethany Louise May 13 '16 at 05:02
  • OP already said what is wanted: *"need to count the number of occurrences for all of the elements in the Arraylist"*. No extra input of what to count, because you have to count what is in the list. – Andreas May 13 '16 at 05:04