Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In an array of words some words are repeated twice(exactly twice), count such words.

sample input:

hate love peace love peace hate love peace love peace.

output:

1

here 'hate' is repeated twice.

What I have tried:

a = input().split()
count = 0
for i in a:
    if i in a:
        count+=1 
print(count)
Posted
Updated 27-Feb-22 6:02am

Stop and think about what you are trying to do instead of trying to dive right into code.

Yes, split it into separate words, but after that you need to work out which words are repeated exactly twice, and there is a trick to that: sorting.
If you sort the array of words, then all the duplicates are right next to eat other ... so counting them becomes pretty trivial:
1) loop through the array once
1.2) Check if the current word is the same as the last.
1.2.1) If it is, increment the "duplicates" count
1.2.2) Otherwise, check the duplicates count.
1.2.2.1) If that is 2, increment the "two of" count, and reset the "duplicates" count.
1.2.2.2) Otherwise, reset the "duplicates" count.
1.3) set the last word to the current, and move on to next word.
2) After the loop, print the "two of" count.

Simple, and pretty efficient.

Give it a try!
 
Share this answer
 
Comments
CPallini 25-Feb-22 10:38am    
5.
Maciej Los 27-Feb-22 11:30am    
5ed!
Python
for i in a:
    if i in a: # you already know that i is in a from the previous line
        count+=1 # so the end result will just be a count of all the words.
 
Share this answer
 
Comments
Maciej Los 27-Feb-22 11:30am    
5ed!
If you can use multiset...
Python
import multiset
l = "hate love peace love peace hate love peace love peace".split()
m = multiset.Multiset(l)
r = [x for x in m if m.get(x,0)==2]
print(r)
 
Share this answer
 
Comments
Maciej Los 27-Feb-22 11:30am    
5ed!
CPallini 28-Feb-22 2:01am    
Thank you!
Quote:
In an array of words some words are repeated twice(exactly twice), count such words.

You have basically 2 options:
1) Sort the list of word (any repeat will be next to the first occurence of a word), then scan the sorted list and count the repeats.
2) scan the unsorted list and build a dictionary. it a word is new, add it to dictionary, if word is already in dictionary, add 1 to number of time it is seen.
 
Share this answer
 
Comments
Maciej Los 27-Feb-22 11:30am    
5ed!
Patrice T 27-Feb-22 12:20pm    
Thank you.
In addition to [Patrice T] solution - suggestion #2, you can achieve that by using below code:

Python
words = "hate love peace love peace hate love peace love peace".split()
uniquewords = dict((word,words.count(word)) for word in set(words))
print(uniquewords)

Prints:
{'peace': 4, 'hate': 2, 'love': 4}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900