给定n个单词的数组。有些单词重复了两次,我们需要计算这些单词。
null
例如:
Input : s[] = {"hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace"};Output : 1There is only one word "hate" that appears twiceInput : s[] = {"Om", "Om", "Shankar", "Tripathi", "Tom", "Jerry", "Jerry"};Output : 2There are two words "Om" and "Jerry" that appeartwice.
来源:亚马逊采访
以下是实施情况:
C++
// C++ program to count all words with count // exactly 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; } |
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 |
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<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 |
Javascript
<script> // Javascript program to count all words with count // exactly 2. // Returns count of words with frequency // exactly 2. function countWords(str, n) { var m = new Map(); for ( var i = 0; i < n; i++) { if (m.has(str[i])) m.set(str[i], m.get(str[i])+1) else m.set(str[i], 1) } var res = 0; m.forEach((value, key) => { if ((value == 2)) res++; }); return res; } // Driver code var s = [ "hate" , "love" , "peace" , "love" , "peace" , "hate" , "love" , "peace" , "love" , "peace" ]; var n = s.length; document.write( countWords(s, n)); </script> |
输出
1
方法2:使用 内置的 Python函数:
- 用数字统计每个单词的频率 柜台 作用
- 频率字典中的遍历
- 检查哪个单词的频率为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
本文由 萨乌米亚提瓦里酒店 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END