Skip to content

Feature request: Return a list of non-duplicates #1095

@LikeLakers2

Description

@LikeLakers2

I'd like to be able to get all the items that only appear once in an iterator.

This is effectively the opposite of Itertools::duplicates - rather than all the items that appear more than once, it only returns the items that appear exactly once.

As a note: I am already able to do this relatively efficiently, using a combination of Itertools::counts and Iterator::filter_map:

use itertools::Itertools; // 0.14.0

fn main() {
    let c = [0, 1, 3, 4, 5, 1, 2, 3, 4, 6];

    let nondupes = c
        .into_iter()
        .counts()
        .into_iter()
        .filter_map(|(k, v)| (v == 1).then_some(k))
        .collect::<Vec<i32>>();

    // Should return 0, 2, 5, 6, though not necessarily in that order
    // (Hashmaps don't guarantee a specific order, but that's fine)
    dbg!(nondupes);
}

but being able to shorten this to c.into_iter().non_duplicates() would still be nice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions