Skip to content

Instantly share code, notes, and snippets.

@farhanf
Created April 11, 2019 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farhanf/700bef0f6f75d3662e4762083bbbc806 to your computer and use it in GitHub Desktop.
Save farhanf/700bef0f6f75d3662e4762083bbbc806 to your computer and use it in GitHub Desktop.

I attemped two solutions because the instructions didn't specify if I could use built-ins or not.

The first solution is non-linear time. It is big O of n squared as the function goes through the values of the first array for every value of the second array until it finds a value that is not in the first array.

The second solution is in linear time because it uses hashtable behind the scenes thanks to swift built in Set(). Assuming it is a good hashing function this will be big O of n where n is the length of the the second array.

I prefer the second function because it is more performant and easier to read. It can be used for any object as long as it is conforms to hashable protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment