# \`Vec::rdedup(\_by(\_key))\`?

**URL:** <https://internals.rust-lang.org/t/vec-rdedup-by-key/22533>\
**Category:** libs\
**Created:** [March 10, 2025, 4:06pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533 "2025-03-10T16:06:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jisu-Woniu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jisu-woniu/32/11150_2.png) [@Jisu-Woniu](https://internals.rust-lang.org/u/Jisu-Woniu)\
**Post date:** [March 10, 2025, 4:06pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/1 "2025-03-10T16:06:35Z")

</div>

`Vec::dedup` related methods "removes **all but the first** of consecutive elements in the vector". Is there a need for a method removes **all but the last** equal items?

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [March 10, 2025, 4:18pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/2 "2025-03-10T16:18:26Z")

</div>

What is the use case?

---

<div class="post-metadata">

**Author:** ![Jisu-Woniu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jisu-woniu/32/11150_2.png) [@Jisu-Woniu](https://internals.rust-lang.org/u/Jisu-Woniu)\
**Post date:** [March 10, 2025, 4:38pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/3 "2025-03-10T16:38:17Z")

</div>

I have a sorted `Vec` like `[(1, 3), (1, 4), (2, 3)]`, each item is a `(price, value)` pair. I want to know for all things of the same price, how much value can I get.

A reasonable thought is `v.dedup_by_key(|current, previous| current.0 == previous.0 && current.1 >= previous.1)`, but it will always remove the previous one according to the doc.

If `rdedup_by_key` is provided, the code above can be used, with `dedup` changed to `rdedup`.

I can call `reverse` -\> `dedup` -\> `reverse` to achieve the same result, but it would be so costly.

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [March 10, 2025, 4:44pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/4 "2025-03-10T16:44:08Z")

</div>

One alternative solution would be sorting them in reverse order.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [March 10, 2025, 5:02pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/5 "2025-03-10T17:02:56Z")

</div>

> [@Jisu-Woniu](#):
>
> I have a sorted `Vec` like `[(1, 3), (1, 4), (2, 3)]`, each item is a `(price, value)` pair. I want to know for all things of the same price, how much value can I get.

It's probably worth while to check out some kind of [`DataFrame`](https://docs.rs/polars/latest/polars/). I don't think it's worthwhile to add this specific table operation to `Vec` on its own, and if multiple such operations were to be added they might be better _grouped by_ a different abstraction altogether.

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [March 10, 2025, 5:36pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/6 "2025-03-10T17:36:57Z")

</div>

> [@Jisu-Woniu](#):
>
> A reasonable thought is `v.dedup_by_key(|current, previous| current.0 == previous.0 && current.1 >= previous.1)`, but it will always remove the previous one according to the doc.

[`Vec::dedup_by`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.dedup_by) gives the closure `&mut`-access to both items, so you're free to modify the item that will be kept.

```rust
v.dedup_by(|next, prev| {
    next.0 == prev.0 && {
        std::mem::swap(next, prev);
        true
    }
})

```

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [March 10, 2025, 7:40pm UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/7 "2025-03-10T19:40:39Z")

</div>

The process for such proposals is to [submit an ACP](https://github.com/rust-lang/libs-team/issues/new?template=api-change-proposal.md).

---

<div class="post-metadata">

**Author:** ![Jisu-Woniu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jisu-woniu/32/11150_2.png) [@Jisu-Woniu](https://internals.rust-lang.org/u/Jisu-Woniu)\
**Post date:** [March 11, 2025, 3:52am UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/8 "2025-03-11T03:52:27Z")

</div>

Oh I really like this idea

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 2, 2026, 3:52am UTC](https://internals.rust-lang.org/t/vec-rdedup-by-key/22533/9 "2026-09-02T03:52:54Z")

</div>

Closed on September 2, 2026, 3:52am UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
