Trait SliceExt

Source
pub trait SliceExt<T> {
    // Required method
    fn has_dup(&self) -> bool
       where T: Hash + Eq;
}
Available on crate feature slice only.
Expand description

Extensions to the slice type.

Required Methods§

Source

fn has_dup(&self) -> bool
where T: Hash + Eq,

Check if the slice contains duplicate elements.

This method returns true if there are any duplicate elements in the slice, and false if all elements are unique.

§Time Complexity

This method has O(n) average time complexity, where n is the length of the slice. In the worst case (with many hash collisions), it could degrade to O(n²), but this is extremely rare with a good hash function.

§Space Complexity

This method uses O(n) additional space to store the seen elements.

§Examples
use est::slice::SliceExt;

let slice_with_dups = [1, 2, 3, 2, 4];
assert!(slice_with_dups.has_dup());

let slice_without_dups = [1, 2, 3, 4, 5];
assert!(!slice_without_dups.has_dup());

let empty_slice: [i32; 0] = [];
assert!(!empty_slice.has_dup());

let single_element = [42];
assert!(!single_element.has_dup());
§Type Requirements

The element type T must implement Hash and Eq traits to be used in the internal HashSet.

Implementations on Foreign Types§

Source§

impl<T> SliceExt<T> for [T]

Source§

fn has_dup(&self) -> bool
where T: Hash + Eq,

Implementors§