algorithm - Array remove duplicate elements -
I have an unreserved array, if present, what is the best way to remove all duplicates of an element?
Example:
one [1,5,2,6,8,9,1,1,10,3,2,4,1,3,11 , 3]
So the array should look after that operation
one [1,5,2,6,8,9,10,3 , 4,11]
Check each element against each other element
The simple solution is to check each element against every other element. It is useless and produces o (n 2 ) solution, even if you only "Forward".
Sort again delete the duplicate
The best solution is to sort the array and then check each element next to it to find duplicates. Select an efficient type and it's O (n log n).
There is no insecurity order with a sort-based solution. An additional step can take care of this. Place all entries (in unique sorted array) in the hashtable, which has O (1) access. Then iterate over the original array. For each element, check whether it is in the hash table. If so, add it to the result and remove it from the hash table. You will end with a resultant array, in which each element will be placed in the same position as its first event in the order of origin.
Linear type of integer
If you can do better by using the Radix sort to deal with the integers of certain boundaries, if you believe that the number 0 to 1,000,000 Between, for example, you can allocate a little vector of some 1,000,001. For each element in the original element, you set the corresponding bit based on its value (such as the value of 13 results in setting 14th bits). Then cross the original array, check whether it is in bit vector or not. If so, add it to the result array and clear that bit with a bit vector. It is O (n) and does trade of space for time.
hash table solution
Which takes us to the best solution: Sorting is actually a distraction, though useful (1) Create a hashtable with access to the original list If it is not already in the hashtable, add it to the result array and add it to the hash table. If it is in the hash table, ignore it.
This is the best solution, why else? Because such problems are about problems that you have (or should) have about the knowledge and are about to refine them based on the solutions that you do in the solution, develop a solution and its Understanding the thinking behind is much more useful than a solution solution.
In addition to this, hash tables are not always available. Take an embedded system or some space where the space is very limited. You can apply a quick sort to a handful of epododes, which is much less than any hash table.
Comments
Post a Comment