Skip to content

Instantly share code, notes, and snippets.

@elvismetaphor
Created May 31, 2018 04:51
Show Gist options
  • Save elvismetaphor/e3d87dc7a6a1cedf86881ba4c961f2d0 to your computer and use it in GitHub Desktop.
Save elvismetaphor/e3d87dc7a6a1cedf86881ba4c961f2d0 to your computer and use it in GitHub Desktop.

Answer

Let the size of the first array is N and the size of the second array is M. The big O of the time complexity of this algorithm is O(N + M)

Reason

In the beginning, we put all elements in the first array into a HashSet object, the time complexity is O(N) because we walk throught all elements, and saving a element into a hash set only costs a constant time.

In order to determine if the second array is a subset of the first array, we walk through all elements in the second array, and check if each element has already been in the hash set. Because finding a element in a hash set also costs a constant time, the time complexity is O(M)

After executing the above steps, we can know if the second array is a subset of the first array. The total time complexity is O(M + N)

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