Two ways.
Sort both arrays. Use pointers to traverse both the arrays from start and print any matching elements.
Or
Use Hashmap storing the array element and its count value from first array.
Sigiloso
12 de mar. de 2011
Since Amazon is particularly fond of hash tables, I'd prefer to use it in the solution. Plus it's slightly faster than sorting both arrays.
public static int[] ArrayIntersection(int[] arr1, int[] arr2)
{
Dictionary hashTable = new Dictionary();
for (int i = 0; i list = new List();
for (int i = 0; i < arr2.Length; i++)
{
if (hashTable.ContainsKey(arr2[i]))
{
list.Add(arr2[i]);
}
}
return list.ToArray();
}