# 5- Data Structures 23 Exercise

**URL:** <https://forum.codewithmosh.com/t/5-data-structures-23-exercise/2984>\
**Category:** Python\
**Created:** [February 24, 2021, 4:40pm UTC](https://forum.codewithmosh.com/t/5-data-structures-23-exercise/2984 "2021-02-24T16:40:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luifrancgom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/luifrancgom/32/753_2.png) [@luifrancgom](https://forum.codewithmosh.com/u/luifrancgom)\
**Post date:** [February 24, 2021, 4:40pm UTC](https://forum.codewithmosh.com/t/5-data-structures-23-exercise/2984/1 "2021-02-24T16:40:16Z")

</div>

Hello world

I was able to answer the exercise using the following code where also Mosh presents an alternative solution using dictionaries:

**sentence = "This is a common interview question"**

**sentence\_unpack = [\*sentence]**

**count = [(sentence\_unpack.count(x), x) for x in sentence\_unpack]**

**max(sorted(list(set(count)), reverse = True), key = lambda x: x[0])**

However this solution and Mosh solution eliminate ties.

Is there a way to solve the exercise without eliminating ties and doing automatically? That is sorting a list with tuples and obtaining the maximum values without eliminating ties. Therefore obtaining (5, ‘i’) and (5, ’ ')

For example I search for the max function and I obtain the following, [Built-in Functions — Python 3.9.2 documentation](https://docs.python.org/3/library/functions.html#max) :

“… _If multiple items are maximal, the function returns the first one encountered_. …”

So I didn’t find I way to find all the maximum values when you obtain ties.

I know this is not efficient when you have something like [5, 3, 4, 5] but in the case of [(5, “a”), (3, “b”), (4, “c”), (5, “e”)] it make sense to obtain all the maximum values if you use only the first component of the tuple to find the maximum values.

---

<div class="post-metadata">

**Author:** ![paulcoder](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@paulcoder](https://forum.codewithmosh.com/u/paulcoder)\
**Post date:** [September 26, 2021, 3:23pm UTC](https://forum.codewithmosh.com/t/5-data-structures-23-exercise/2984/2 "2021-09-26T15:23:12Z")

</div>

Hey, did you ever solve this? I have been trying to have a go at Unit 5, Vid 23 like you and am having some trouble.

---

<div class="post-metadata">

**Author:** ![Salurex](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Salurex](https://forum.codewithmosh.com/u/Salurex)\
**Post date:** [September 13, 2022, 4:13pm UTC](https://forum.codewithmosh.com/t/5-data-structures-23-exercise/2984/3 "2022-09-13T16:13:20Z")

</div>

Dunno if that is right.  
That is my first try :

sentence = “This is a common interview question”  
winner = " "  
summary = 0  
for item in sentence.replace(" ", “”):  
number = sentence.count(item)  
if number \> summary:  
summary += number  
winner = item, number

print(winner)
