# Frequency of a string in an array of strings

You are given a collection of strings and a list of queries. For every query there is a string given. We need to print the number of times the given string occurs in the collection of strings.
Examples:

```Input : arr[] = {wer, wer, tyu, oio, tyu}
q[] =   {wer, tyu, uio}
Output : 2 2 0
Explanation :
q appears two times in arr[], q1[] appears
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Simple)

The idea is simple, for every query string we compare it with all strings given in array. If the query string is matches, we increment count.

## Java

 `// Java program to find number of times a string ` `// appears in an array. ` `class` `SubString ` `{ ` `    ``/* Returns count of occurrences of s in arr[] */` `    ``static` `int` `search(String[]arr, String s) ` `    ``{ ` `            ``int` `counter = ``0``; ` `            ``for` `(``int` `j = ``0``; j < arr.length; j++) ` ` `  `                ``/* checking if string given in query is ` `                  ``present in the given string. If present, ` `                  ``increase times*/` `                ``if` `(s.equals(arr[j])) ` `                    ``counter++; ` ` `  `           ``return` `counter; ` `    ``} ` ` `  `    ``static` `void` `answerQueries(String[] arr, String q[]) ` `    ``{ ` `        ``for` `(``int` `i=``0``;i

## Python3

 `# Python3 program to find number of  ` `# times a string appears in an array. ` ` `  `# Returns count of occurrences of s in arr[]  ` `def` `search(arr, s): ` `    ``counter ``=` `0` `    ``for` `j ``in` `range``(``len``(arr)): ` `         `  `        ``# checking if string given in query  ` `        ``# is present in the given string.   ` `        ``# If present, increase times ` `        ``if` `(s ``=``=` `(arr[j])): ` `            ``counter ``+``=` `1` ` `  `    ``return` `counter ` ` `  `def` `answerQueries(arr, q): ` `    ``for` `i ``in` `range``(``len``(q)): ` `        ``print``(search(arr, q[i]),  ` `                     ``end ``=` `" "``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``"aba"``, ``"baba"``, ``"aba"``, ``"xzxb"``] ` `    ``q ``=` `[``"aba"``, ``"xzxb"``, ``"ab"``] ` `    ``answerQueries(arr, q) ` ` `  `# This code is contributed  ` `# by PrinciRaj19992 `

## C#

 `// C# program to find number of  ` `// times a string appears in an array. ` `using` `System; ` ` `  `class` `SubString ` `{ ` `    ``/* Returns count of occurrences of s in arr[] */` `    ``static` `int` `search(String[]arr, String s) ` `    ``{ ` `            ``int` `counter = 0; ` `            ``for` `(``int` `j = 0; j < arr.Length; j++) ` ` `  `                ``/* checking if string given in query is ` `                ``present in the given string. If present, ` `                ``increase times*/` `                ``if` `(s.Equals(arr[j])) ` `                    ``counter++; ` ` `  `        ``return` `counter; ` `    ``} ` ` `  `    ``static` `void` `answerQueries(String []arr, String []q) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < q.Length; i++) ` `            ``Console.Write(search(arr, q[i]) + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``String []arr = {``"aba"``,``"baba"``,``"aba"``,``"xzxb"``}; ` `        ``String []q = {``"aba"``,``"xzxb"``,``"ab"``}; ` `        ``answerQueries(arr, q); ` `    ``} ` `} ` ` `  `//This code is contributed by nitin mittal `

## PHP

 ` `

Output:

```2 1 0
```

Method 2 (Using Trie)

Trie an efficient data structure used for strong and retrieval of data like strings. The searching complexity is optimal as key length.
In this solution we insert the collection of strings in the Trie data structure so they get stored in it. One important thing is, we keep count of occurrences in leaf nodes. Then we search the Trie for the given query string and check if it is present in the Trie.

 `// C++ program to count number of times ` `// a string appears in an array of strings ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX_CHAR = 26; ` ` `  `struct` `Trie ` `{ ` `    ``// To store number of times ` `    ``// a string is present. It is ` `    ``// 0 is string is not present ` `    ``int` `cnt; ` ` `  `    ``Trie *node[MAX_CHAR]; ` `    ``Trie() ` `    ``{ ` `        ``for``(``int` `i=0; inode[index]) ` `            ``root->node[index] = ``new` `Trie(); ` ` `  `        ``root = root->node[index]; ` `    ``} ` `    ``root->cnt++; ` `    ``return` `temp; ` `} ` ` `  `/* Returns count of occurrences of s in Trie*/` `int` `search(Trie *root, string s) ` `{ ` `    ``int` `n = s.size(); ` `    ``for` `(``int` `i=0; inode[index]) ` `            ``return` `0; ` `        ``root = root->node[index]; ` `    ``} ` `    ``return` `root->cnt; ` `} ` ` `  `void` `answerQueries(string arr[], ``int` `n, string q[], ` `                                           ``int` `m) ` `{ ` `    ``Trie *root = ``new` `Trie(); ` ` `  `    ``/* inserting in Trie */` `    ``for` `(``int` `i=0; i

Output:

```2
1
0
```

Method 3 (Hashing)

We can use a hash map and insert all given strings into it. For every query string, we simply do a look-up in the hash map.

Please refer Data Structure for Dictionary for comparison of hashing and Trie based solutions.

This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.