Distinguishing extra element from two arrays?

后端 未结 19 691
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  一个人的身影
    2020-12-28 10:27

    How about getting the total sum for each of the arrays and subtracting one from the other? The difference is the extra element.

    eg: A = 1,2,3,4,5 B = 1,2,3,4

    sum(A) = 15, sum(B) = 10; 15 - 10 = 5 which is the extra element.

提交回复
热议问题