VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-words-appear-exactly-two-times-array-words/

⇱ Count words that appear exactly two times in an array of words - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count words that appear exactly two times in an array of words

Last Updated : 23 Mar, 2023

Given an array of n words. Some words are repeated twice, we need to 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

Recommended Practice

Below is the implementation: 


Output
1

Time Complexity : O(N)
Auxiliary Space: O(N)

Method 2: Using Built-in Python functions:

  • Count the frequencies of every word using the Counter function
  • Traverse in frequency dictionary
  • Check which word has frequency 2. If so, increase the count
  • Print Count

Below is the implementation:


Output
1

Time Complexity : O(n)
Auxiliary Space: O(n)

Comment