Yelp Interview Question

How to find the only different number in two unsorted arrays?

Interview Answers

Anonymous

Mar 28, 2014

You can use xor

12

Anonymous

Jun 19, 2014

Why not just sum both arrays up the difference will be the different no.

9

Anonymous

Mar 13, 2014

Using HashTable to count the occurrence of each number

4

Anonymous

Mar 1, 2015

In Python: from collections import Counter def diffNum(a,b): aCount = Counter(a) for x in b: if x not in aCount: return x return None a = [1,3,4,5,2] b = [1,2,7,5,4,3] print diffNum(a,b)

1

Anonymous

May 1, 2015

Given 2 lists a and b with one element that is different, sum(a) - sum(b) will give you the element

1

Anonymous

Mar 15, 2014

Here is the C# code with HashTable class Class1 { public int Different_Number(int[] A, int[] B) { if (A.Length < B.Length) { return Hash_It(A, B); } else { return Hash_It(B, A); } } private int Hash_It(int[] A, int[] B) { Hashtable ht = new Hashtable(); for (int i = 0; i < A.Length; i++) { ht.Add(A[i], i); } for (int i = 0; i < B.Length; i++) { if (!ht.Contains(B[i])) { return B[i]; } } return null; } public static void Main() { int[] A = new int[] { 1,5,3,8}; /* Unsorted array */ int[] B = new int[] { 5,3,8,2102,1}; Class1 cls = new Class1(); Console.WriteLine(cls.Different_Number(A, B)); Console.ReadLine(); } }