计算单词数组中恰好出现两次的单词
给定一个包含 n 个单词的数组。有些单词重复了两次,我们需要对这些单词进行计数。
例子:
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.
资料来源:亚马逊采访
下面是实现:
C++
// C++ program to count all words with count
// exactly 2.
#include
using namespace std;
// Returns count of words with frequency
// exactly 2.
int countWords(string str[], int n)
{
unordered_map 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;
}
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 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 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
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
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 m = new Dictionary();
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 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
Javascript
Python
# importing Counter from collections
from collections import Counter
# Python program to count all words with count exactly 2.
# Returns count of words with frequency exactly 2.
def countWords(stri, n):
# Calculating frequency using Counter
m = Counter(stri)
count = 0
# Traversing in freq dictionary
for i in m:
if m[i] == 2:
count += 1
return count
# 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 vikkycirus
输出
1
方法2:使用内置Python函数:
- 使用Counter函数计算每个单词的频率
- 在频率字典中遍历
- 检查哪个单词的频率为 2。如果是,增加计数
- 打印计数
下面是实现:
Python
# importing Counter from collections
from collections import Counter
# Python program to count all words with count exactly 2.
# Returns count of words with frequency exactly 2.
def countWords(stri, n):
# Calculating frequency using Counter
m = Counter(stri)
count = 0
# Traversing in freq dictionary
for i in m:
if m[i] == 2:
count += 1
return count
# 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 vikkycirus
输出
1