Given an array of n words. Some words are repeated twice, we need count such words.
Examples:
Input : s[] = {"hate", "love", "peace", "love",
"peace", "hate", "love", "peace",
"love", "peace"};
Output : 1
There is only one word "hate" that appears twice
Input : s[] = {"Om", "Om", "Shankar", "Tripathi",
"Tom", "Jerry", "Jerry"};
Output : 2
There are two words "Om" and "Jerry" that appear
twice.
Source : Amazon Interview
- Traverse the given array, store counts of words in a hash table
- Traverse hash table and count all words with count 2.
Below is the implementation.
C++
// C++ program to count all words with count // exavtly 2. #include <bits/stdc++.h> using namespace std; // Returns count of words with frequency // exactly 2. int countWords(string str[], int n) { unordered_map<string, int> m; for (int i = 0; i < n; i++) m[str[i]] += 1; int res = 0; for (auto it = m.begin(); it != m.end(); it++) if ((it->second == 2)) res++; return res; } // Driver code int main() { string s[] = { "hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace" }; int n = sizeof(s) / sizeof(s[0]); cout << countWords(s, n); return 0; } |
chevron_right
filter_none
Java
// Java program to count all words with count // exactly 2. import java.util.HashMap; import java.util.Map; public class GFG { // Returns count of words with frequency // exactly 2. static int countWords(String str[], int n) { // map to store count of each word HashMap<String, Integer> m = new HashMap<>(); for (int i = 0; i < n; i++){ if(m.containsKey(str[i])){ int get = m.get(str[i]); m.put(str[i], get + 1); } else{ m.put(str[i], 1); } } int res = 0; for (Map.Entry<String, Integer> it: m.entrySet()){ if(it.getValue() == 2) res++; } return res; } // Driver code public static void main(String args[]) { String s[] = { "hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace" }; int n = s.length; System.out.println( countWords(s, n)); } } // This code is contributed by Sumit Ghosh |
chevron_right
filter_none
Python3
# Python program to count all # words with count # exactly 2. # Returns count of words with frequency # exactly 2. def countWords(stri, n): m = dict() for i in range(n): m[stri[i]] = m.get(stri[i],0) + 1 res = 0 for i in m.values(): if i == 2: res += 1 return res # Driver code s = [ "hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace" ] n = len(s) print(countWords(s, n)) # This code is contributed # by Shubham Rana |
chevron_right
filter_none
C#
// C# program to count all words with count // exactly 2. using System; using System.Collections.Generic; class GFG { // Returns count of words with frequency // exactly 2. static int countWords(String []str, int n) { // map to store count of each word Dictionary<String,int> m = new Dictionary<String,int>(); for (int i = 0; i < n; i++) { if(m.ContainsKey(str[i])) { int get = m[str[i]]; m.Remove(str[i]); m.Add(str[i], get + 1); } else { m.Add(str[i], 1); } } int res = 0; foreach(KeyValuePair<String, int> it in m) { if(it.Value == 2) res++; } return res; } // Driver code public static void Main(String []args) { String []a = { "hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace" }; int n = a.Length; Console.WriteLine( countWords(a, n)); } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
Output:
1
This article is contributed by Saumya Tiwari. 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Remove elements from the array which appear more than k times
- Remove characters that appear more than k times
- Remove elements that appear strictly less than k times
- Largest sub-string where all the characters appear at least K times
- Largest substring where all characters appear at least K times | Set 2
- Array elements that appear more than once
- Largest element in the array that is repeated exactly k times
- Check if all occurrences of a character appear together
- Distinct state codes that appear in a string as contiguous sub-strings
- String which when repeated exactly K times gives a permutation of S
- Smallest element repeated exactly ‘k’ times (not limited to small range)
- Number of sub-strings that contain the given character exactly k times
- Find Nth smallest number that is divisible by 100 exactly K times
- Remove characters from a String that appears exactly K times
- Smallest String consisting of a String S exactly K times as a Substring
- Check if the given string of words can be formed from words present in the dictionary
- Count number of substrings with exactly k distinct characters
- Count of pairs of strings which differ in exactly one position
- Count of substrings of length K with exactly K distinct characters
- Count root to leaf paths having exactly K distinct nodes in a Binary Tree
Improved By : Rajput-Ji

