Skip to content
Advertisement

Output Common Elements of Two Lists using Sets

I’d like to write a function definition that takes two lists and returns a single set with the values that each list has in common.

Formally, here are some assertions I’d like to pass:

JavaScript

Here’s the code I’m having trouble with:

JavaScript

I receive an error output that I know has to do with strings and integers, but am unsure what exactly the problem is. (Maybe it’s that I make the above list by saying list = ""?):

JavaScript

Advertisement

Answer

If you want to use append() then you have to use list

JavaScript

If you use string list = "" then you can only concatenate strings

JavaScript

But you need set as answer then you should use list = set() and add(a)

JavaScript

But you can do the same shorter

JavaScript

And this is why set() can be very useful.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement