Interview Questions

Merge two unsorted array and remove the duplicate from........

Microsoft Interview Questions and Answers


(Continued from previous question...)

118. Merge two unsorted array and remove the duplicate from........

Question:
Merge two unsorted array and remove the duplicate from the resultant array.
eg) Array1 = {"are","you","there"}
Array2={"how","are","you"}
output={"how","are","you","there"}



maybe an answer:
1) Take array 1 push all the elements into the Table.
2) Take array 2 before we push keep checking if the key already exists if it does discard the element of array 2. Else insert into the table
3) Print out values in the table.


maybe an answer2:


string[] mergeUnsortedArrayusingHashTable(string[] array1, string[] array2)
{
int arr1len = array1.Length, arr2len = array2.Length;
int i = 0;
string[] outArray = new string[arr1len + arr2len];

Hashtable ht = new Hashtable();
//insert all the element from first array
for (i = 0; i < arr1len; i++)
{
if (!ht.Contains(array1[i]))
ht.Add(array1[i], array1[i]);
}
//insert all the element from second array
for (i = 0; i < arr2len; i++)
{
if (!ht.Contains(array2[i]))
ht.Add(array2[i], array2[i]);
}
i = 0;
foreach (object key in ht.Keys)
{
outArray[i++] = key.ToString();
}
return outArray;
}

(Continued on next question...)

Other Interview Questions