counting duplicates in c++

前端 未结 4 1600
别跟我提以往
别跟我提以往 2021-01-14 03:35

Let\'s say I have an array of ints {100, 80, 90, 100, 80, 60}

so I want to count those duplicates and save those counter for later. because each duplicate number sh

4条回答
  •  心在旅途
    2021-01-14 03:58

    Use a std::map or std::unordered_map for counting the occurences.

    Then iterate over the map and replace each value by the key divided by the original value (counter).

    Finally go through the original array and replace each number by its mapped value.

    If you use a std::unordered_map the algorithm is O(n). Your original one is O(n log n) because sorting is involved.

提交回复
热议问题