-3

In my Android app I have a String[] initialized like this:

String[] array = {"abc", "def", "abc", "ghi", "def", "def"};

How can I check how many times every String appears in the String[]? For example, "abc" appears 2 times, "def" appears 3 times, and "ghi" appears only once.

I'll appriciate it if you'll help me to figure it out. Thanks!

Ido
  • 129
  • 2
  • 11

1 Answers1

1

Check this :

public int checkOccurrence(String word, String [] array){

    int count=0;

    for (int i=0; i<array.length; i++){

        if(word.equals(array[i]))
            count++;
    }

    return count;
}
Vygintas B
  • 1,624
  • 13
  • 31