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:

  1. create hash map frequency of each number in array
  2. go through array , o(1) lookup in hash map. if frequency > 0, remove number array.

is there more efficient way?

another option

  1. sort array o(nlog n) using quick sort or merge sort
  2. 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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -