algorithm - how to remove duplicate numbers from unsorted array -
i given following question in technical interview:
how remove duplicates unsorted array?
one option thinking of:
- create hash map frequency of each number in array
- go through array , o(1) lookup in hash map. if frequency > 0, remove number array.
is there more efficient way?
another option
- sort array o(nlog n) using quick sort or merge sort
- then iterate through array , remove duplicates
why option 1 better option 2?
i cannot use functions work array_unique.
instead of removing object array if hash map says there duplicate, why don't build new array each item in hash map, , add array if there isn't duplicate? idea save step of having 2 arrays equal overhead @ start. php sucks @ garbage collection if start massive array, though unset value, might still hanging around in memory.
Comments
Post a Comment