Std Trait or impl for checking duplicate item

let dup_member = vec![ 2, 0, 1, 2, 3, 4, 5 ]; 


this code below has an example.

use std::usize;

// no sorting related to spesific impl (audio multi channels)
fn check_valid_channels(list: &Vec<u8>, chnumber: u8) -> bool {
    // part of my code
    let mut result = true;
    if (chnumber -1) as usize > list.len() {
        // out of range
        result = false;
    } else {
        let mut i: usize = 0;

        // begin this code i want to simplyfy
        while i < chnumber as usize {

            let value0 = list[i];
            let mut j: usize = (chnumber -1) as usize;

            while j > i {
                // println!("value0:{}, list[{}]: {}", value0, j, list[j]);
                if value0 == list[j] {
                    result = false;
                }
                j = j - 1;
            }

            i = i + 1;
        }
        // end
    }

    result
}

fn main() {
    // let mapcfg7: Vec<u8> = vec![2, 6, 7, 0, 1, 4, 5, 3];
    let mapcfg7: Vec<u8> = vec![2, 6, 7, 0, 1, 7, 5, 3];
    println!("is valid chan order: {}", check_valid_channels(&mapcfg7, 8));
}

is rust standard library have traits for check member in vector was duplicated or not?.

Is there any particular reason why you need a trait? Are you trying to write generic code? You can solve this problem with several non-trait methods of Vec and/or slices and/or HashMap, etc., given let mut dup_member = …:

  1. let old_len = dup_member.len();
    dup_member.sort();
    dup_member.dedup();
    let new_len = dup_member.len();
    if new_len < old_len {
        // there were duplicates
    }
    
  2.  let mut counts = HashMap::with_capacity(dup_member.len());
     for x in &dup_member {
         *counts.entry(&x).or_insert(0) += 1;
     }
     // here `counts` contains the number of occurrences of each
     // number, if you need that. If you only want to count until
     // you find a single duplicate, see the next item:
    
  3.  let mut set = HashSet::with_capacity(dup_member.len());
     for &x in &dup_member {
         if !set.insert(x) {
             // There was a duplicate. Handle it, and then:
             break;
         }
     }
    
  4.  use itertools::Itertools;
    
     dup_member.sort();
     let groups = dup_member.into_iter().group_by(Clone::clone);
    
     for (key, group) in &groups {
         // `group` is an iterator, you can check whether it has
         // a `.count()` of 1 or more, for example:
         if group.count() > 1 { /* handle duplicate */ }
     }
    
2 Likes

for some reason i want simplify my code

Generics do not always simplify your code. If you only ever need to call it with a vector, it is almost certainly simpler to write a method that only works for vectors than one which works for more types.

Perhaps you are asking if the standard library has a trait included for performing this operation? The answer is no.

3 Likes

yes something like that.

but before and after I am very grateful that this community helps a lot with the rust-lang learning process.

1 Like