How to remove duplicate data from an array efficiently?.......
Microsoft Interview Questions and Answers
(Continued from previous question...)
114. How to remove duplicate data from an array efficiently?.......
Question:
How to remove duplicate data from an array efficiently? Provide more solutions in the form with additional memory with O(n), O(n2) and nlog2n.
maybe an answer:
// O(n) solution
void RemoveDup(int[] array)
{
int dst = 0;
Dictionary<int, bool> dic = new Dictionary<int, bool>();
for (int i = 0; i < array.Length; i++)
{
if (!dic.ContainsKey(array[i]))
{
array[dst] = array[i];
dic.Add(array[i], true);
dst++;
}
}
for (int j = dst; dst < array.length; dst ++)
{
//mark as invalid element. clear out the array
}
}
(Continued on next question...)
Other Interview Questions
|