Click here to Skip to main content
15,077,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using python language.
say the list = [a, b, a, e, g, a, b]
so, the output should be: a, b

What I have tried:

I have tried this algorithm:

1- make a dictionary and put the characters without repeating. #{a, b, e, g}
2- list = []
3- for each character in the dictionary:
if the character doesn't exist in the list:
count the character and append it to the list
4- sort the list, with reverse
5- print the first two characters

Will it be O(n)?
and if not, how will I make an algorithm that solve this problem with the possible less time complexity?
Posted
Updated 7-May-21 2:58am

1 solution

Well ... what is time consuming here?
What is the time complexity of the sort operation?

Do you need to sort it?
Or could you find the two maximal values from a list in a single pass through the data?

Could you combine the operations so you only need a single pass though the input list? Do you need a dictionary and a list?

This is homework so I won't answer those questions for you - just leave them there for you to think about.
   
Comments
CPallini 7-May-21 9:05am
   
5.
Michel Jakson 7-May-21 9:30am
   
If the original question was finding frequencies in n*m matrix, i will not convert it to a single list to do the above steps right?
OriginalGriff 7-May-21 9:55am
   
Why would you? Do you think it's needed?

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