How to efficiently remove duplicates from an array without using Set

后端 未结 30 2538
情深已故
情深已故 2020-11-22 07:29

I was asked to write my own implementation to remove duplicated values in an array. Here is what I have created. But after tests with 1,000,000 elements it took very long ti

30条回答
  •  臣服心动
    2020-11-22 07:35

    There exists many solution of this problem.

    1. The sort approach

      • You sort your array and resolve only unique items
    2. The set approach

      • You declare a HashSet where you put all item then you have only unique ones.
    3. You create a boolean array that represent the items all ready returned, (this depend on your data in the array).

    If you deal with large amount of data i would pick the 1. solution. As you do not allocate additional memory and sorting is quite fast. For small set of data the complexity would be n^2 but for large i will be n log n.

提交回复
热议问题