Distinguishing extra element from two arrays?

后端 未结 19 686
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 09:45

One of my friend was asked this question in an interview -

  • You have given two integer arrays each of size 10.
  • Both contains 9 equal elements (say 1 t
19条回答
  •  猫巷女王i
    2020-12-28 10:41

    Two passes through the array will suffice.

    1st pass: add every item in the shorter list to a hashmap (dict in python). 2nd pass: for each item in the longer list, check if key exist in hashmap (O(1) search time). If not, that key is the unique entry.

    Total time complexity: O(2n) = O(n)

提交回复
热议问题