How to find the first repeated character in a string with Rust

3 Answers

0 votes
fn get_first_repeated_character(s: &str) -> char {
    // Collect chars into a vector for O(1) index access and clean types
    let chars: Vec<char> = s.chars().collect();
    let len = chars.len();
 
    for i in 0..len {
        for j in 0..i {
            if chars[i] == chars[j] {
                return chars[i];
            }
        }
    }
 
    ' ' // Return a space if no character repeats
}

fn main() {
    let s = "abcdxypcbop";
     
    let result = get_first_repeated_character(s);
     
    // Fixed: matched the check against ' ' (the no-match return value)
    if result != ' ' {
        println!("First repeated character: {}", result);
    } else {
        println!("No repeated characters");
    }
}
 
    
/*
run:
   
First repeated character: c
   
*/

 



answered Dec 2, 2024 by avibootz
edited 6 days ago by avibootz
0 votes
use std::collections::HashSet;

/// Finds the first repeated character in a string.
///
/// Examples:
/// - "abca"         -> Some('a')
/// - "abcbd"        -> Some('b')
/// - "abcde"        -> None
/// - "abcdxypcbop"  -> Some('c')
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
fn find_first_repeated_character(input: &str) -> Option<char> {
    let mut seen: HashSet<char> = HashSet::new();

    for ch in input.chars() {
        // HashSet::insert returns false if the value
        // was already present in the set.
        if !seen.insert(ch) {
            return Some(ch);
        }
    }

    // No repeated character found.
    None
}

fn main() {
    println!("{:?}", find_first_repeated_character("abca"));         
    println!("{:?}", find_first_repeated_character("abcbd"));        
    println!("{:?}", find_first_repeated_character("abcde"));        
    println!("{:?}", find_first_repeated_character("abcdxypcbop"));  
}
 
    
/*
run:
   
Some('a')
Some('b')
None
Some('c')
   
*/

 



answered 6 days ago by avibootz
0 votes
use std::collections::HashSet;

/// Finds the first repeated character in a string.
///
/// Examples:
/// - "abca"         -> Some('a')
/// - "abcbd"        -> Some('b')
/// - "abcde"        -> None
/// - "abcdxypcbop"  -> Some('c')
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
fn find_first_repeated_character(input: &str) -> Option<char> {
    let mut seen: HashSet<char> = HashSet::new();

    for ch in input.chars() {
        // HashSet::insert returns false if the value
        // was already present in the set.
        if !seen.insert(ch) {
            return Some(ch);
        }
    }

    // No repeated character found.
    None
}

fn print_result(input: &str) {
    match find_first_repeated_character(input) {
        Some(ch) => println!("{}", ch),
        None => println!("No repeated character"),
    }
}

fn main() {
    print_result("abca");
    print_result("abcbd");
    print_result("abcde");
    print_result("abcdxypcbop");
}
 
    
/*
run:
   
a
b
No repeated character
c
   
*/

 



answered 6 days ago by avibootz
...