Making Python code faster for large data sets

I am solving a problem on HackerEarth (full text can be found here):

Bob has a playlist of songs, each song has a singer associated with it (denoted by an integer)

Favourite singer of Bob is the one whose songs are the most on the playlist

Count the number of Favourite Singers of Bob

For example with [1, 1, 2, 2, 3] as input, the output should be 2 since both 1 and 2 as the most common.

The problem is that my code passes all the test cases except for the ones which have large data sets, this violates the time constraints. Certainly, I need to optimise my code further.
Here’s my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>singer_tokens = input()
singer_tokens = singer_tokens.split(' ')
fav_singers = []
result = []
for i in singer_tokens[:]:
if i not in result:
result.append(i)
for i in result:
fav_singers.append(singer_tokens.count(i))
max_elem = max(fav_singers)
print(fav_singers.count(max_elem))
</code>
<code>singer_tokens = input() singer_tokens = singer_tokens.split(' ') fav_singers = [] result = [] for i in singer_tokens[:]: if i not in result: result.append(i) for i in result: fav_singers.append(singer_tokens.count(i)) max_elem = max(fav_singers) print(fav_singers.count(max_elem)) </code>
singer_tokens = input()
singer_tokens = singer_tokens.split(' ')

fav_singers = []
result = []

for i in singer_tokens[:]:
    if i not in result:
        result.append(i)

for i in result:
    fav_singers.append(singer_tokens.count(i))


max_elem = max(fav_singers)

print(fav_singers.count(max_elem))

New contributor

user26346636 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

bottlenecks and improvements

Your code has two main inefficient steps.

Here you are searching in the list of unique values for each new item.
Once result starts to be large, this search will be inefficient

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for i in singer_tokens[:]:
if i not in result: # this is inefficient
result.append(i)
</code>
<code>for i in singer_tokens[:]: if i not in result: # this is inefficient result.append(i) </code>
for i in singer_tokens[:]:
    if i not in result:   # this is inefficient
        result.append(i)

This step is done efficiently using a set in python:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>result = set(singer_tokens)
</code>
<code>result = set(singer_tokens) </code>
result = set(singer_tokens)

Then you search each unique value in the full input, this means that you will have to read again the full list for each unique value.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for i in result:
fav_singers.append(singer_tokens.count(i))
</code>
<code>for i in result: fav_singers.append(singer_tokens.count(i)) </code>
for i in result:
    fav_singers.append(singer_tokens.count(i))

You could use a dictionary to keep track of the values. This way you won’t even have to know the list of unique values in advance:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>counts = {}
for s in singer_tokens:
counts[s] = counts.get(s, 0) + 1
</code>
<code>counts = {} for s in singer_tokens: counts[s] = counts.get(s, 0) + 1 </code>
counts = {}
for s in singer_tokens:
    counts[s] = counts.get(s, 0) + 1

Or, better, using collections.Counter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from collections import Counter
counts = Counter(singer_tokens)
</code>
<code>from collections import Counter counts = Counter(singer_tokens) </code>
from collections import Counter

counts = Counter(singer_tokens)

Then to get the number of maxima:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>counts = list(Counter(singer_tokens).values())
print(counts.count(max(counts)))
</code>
<code>counts = list(Counter(singer_tokens).values()) print(counts.count(max(counts))) </code>
counts = list(Counter(singer_tokens).values())

print(counts.count(max(counts)))

batteries included

Now, there’s an even better approach since python is delivered with batteries included, using statistics.multimode which will return the list of most frequent values:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from statistics import multimode
print(len(multimode(singer_tokens)))
</code>
<code>from statistics import multimode print(len(multimode(singer_tokens))) </code>
from statistics import multimode

print(len(multimode(singer_tokens)))

Output: 2

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật