counting duplicates in c++

前端 未结 4 1640
别跟我提以往
别跟我提以往 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:51

    Here is algo to replace elements in place if you are allowed to modify sequence:

    const auto begin = std::begin( data );
    const auto end = std::end( data );
    std::sort( begin, end );
    for( auto it = begin; it != end; ) {
        auto next = std::upper_bound( std::next( it ), end, *it );
        auto newval = *it / std::distance( it, next );
        std::fill( it, next, newval );
        it = next;
    }
    

    demo on ideone

    PS modified to make it compile with array as well.

提交回复
热议问题