Distinguishing extra element from two arrays?

后端 未结 19 583
爱一瞬间的悲伤
爱一瞬间的悲伤 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:34

    In LINQ:

    var unique1 = (from a in arrayA where !arrayB.Contains(a) select a).First();
    var unique2 = (from b in arrayB where !arrayA.Contains(b) select b).First();
    return new Pair(unique1, unique2);
    
    ...
    
    public sealed class Pair
    {
        public T0 Item1 {get;set;}
        public T1 Item2 {get;set;}
        public Pair(T0 item1, T1 item2)
        {
            Item1 = item1;
            Item2 = item2;
        }
        //plus GetHashCode, equality etc.
    }
    

提交回复
热议问题