Algorithm: efficient way to remove duplicate integers from an array

前端 未结 30 2804
离开以前
离开以前 2020-11-22 16:03

I got this problem from an interview with Microsoft.

Given an array of random integers, write an algorithm in C that removes duplicated numbers an

30条回答
  •  盖世英雄少女心
    2020-11-22 16:15

    This can be done in a single pass, in O(N) time in the number of integers in the input list, and O(N) storage in the number of unique integers.

    Walk through the list from front to back, with two pointers "dst" and "src" initialized to the first item. Start with an empty hash table of "integers seen". If the integer at src is not present in the hash, write it to the slot at dst and increment dst. Add the integer at src to the hash, then increment src. Repeat until src passes the end of the input list.

提交回复
热议问题