Challenge 18 Megathread

Welcome to challenge #18!

Use this thread for any and all questions relating to challenge 18

4 Likes

THAT was a doozy!
I look forward to seeing all the ways people go about solving this one. I really doubt my method was remotely expected, but it works. I expect it would not handle a tie well.

[Edit: After some external testing. My method handles a tie by choosing whichever came first in the index order.]

6 Likes

Thanks LHL, had fun with this one!

3 Likes

I’m surprised this one wasn’t worth 50 points! What a pain in the bum :grin:

3 Likes

How did you do it? I just made an empty object, then put in unique stings from the list as keys and gave them their count as the value. Then sorted the object and sent the 0th index

1 Like

The fact that the hints included a link to working with Objects makes me think that a better option is feasible. I did not use objects to solve this. Mine is definitely not the most polished solution, for sure.

I had to make a count(array, item) function using array.filter() because I couldn’t find an array.count() method. :smile:

My solution wouldn’t have worked if there were an unknown number of dinner options on the menu.

Spoiler

Because there are only two dinner options, I found the first element of the list that is different from the one at index zero. Then compared the length of the resulting arrays after using filter() for each dinner option, and returned the largest.

This was a fun challenge and got me thinking of objects more like dictionaries with key value pairs than my usual standard set “class” view from Java or similar languages!

How did you sort the object keys? I looked for a method to do this but on cursory glance couldn’t find one.

This one was interesting. The problem is a subset of the “Majority Element” types of problems. Dictonaries are a good way to count occurences efficiently.

If anyone is interested, there is a clever way to solve this using Boyer-Moore Voting Algorithm

8 Likes

Can you post the logic, not the code, of your solution? I’m curious.

Neat! Thanks, I’m going to modify my code to use this algorithm!

EDIT:
based on the requirements stated in the problem, I removed the check for n/2. Thanks again!

Not the fellow you asked, but I managed to go about this two ways…

  1. I think this may be similar to yours? - set up two arrays using the filter function, then using an if statement compared the length of one of the arrays to the original /2.

  2. My first attempt, bit less elegant, and many more lines… created a variable to store the count of each order type AND a variable for each order name. Looped through the original array and incremented the count depending on array item. Compare the count versus the (original array length / 2), and returned whichever string.

EDIT: In hindsight, I don’t know why I’m comparing against the original array length… if a>b, return a, else return b makes more sense and is simpler. Oops.

1 Like

Yes, very similar to what I came up with.

list.filter(x => x == menuItem).length ≥ list.filter(x => x == otherMenuItem).length

1 Like

Was able to solve this using a nested for loop to create an array of objects, and an second for loop to find which object had most votes.

Stared at screen for over an hour before getting anywhere though haha.

1 Like

I definitely made this one more complicated than it needed to be… Once I made it work I had a good laugh at how complex I was attempting to me the code!

Try console.log(uniquelistOfChoices); after line 5 and see what that line is actually building

Edit: actually, it looks like maybe you just need i < listOfChoices.length on line 6

1 Like

I created an object of arrays.
obj{menuItem:[],votes[]}
Iterated through the votes
Validated whether or not the item existed in the menu list.
If it did not exist, I pushed it into the menu and pushed a vote initialized at 1.
If it did exist, I verified what the index within the menuItem was for that item, and iterated a vote[index]++ to add one vote to that item.

Once I was done iterating through the provided list I had both dynamically built my menu, and collected the votes.

Then I just needed to do a simple find the index with the most votes.
Then return the menuItem at that index.

2 Likes

I chose to go the route of using the reduce() array method to go through the entries and create an object with "name": count pairs. I then used values and keys methods of Object to compare the counts and return the higher value respectively.

My solution will only work with two voting options though a sort method could be used in instances where there are more than two voting options.

1 Like

It is a pity that you deleted your question. I’ve learnt about new Set( ) from you.

2 Likes