Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,082 questions

55,955 answers

573 users

How to find repeated rows of a matrix in Rust

1 Answer

0 votes
use std::collections::HashMap;

fn row_to_string(row: &[i32]) -> String {
    row.iter().map(|num| num.to_string()).collect::<Vec<String>>().join(",")
}

fn find_repeated_rows(matrix: &[Vec<i32>]) {
    let mut row_count: HashMap<String, i32> = HashMap::new();

    for row in matrix {
        let pattern = row_to_string(row);
        *row_count.entry(pattern).or_insert(0) += 1;
    }

    println!("Repeated Rows:");
    for (pattern, count) in &row_count {
        if *count > 1 {
            println!("Row: [{}] - Repeated {} times", pattern, count);
        }
    }
}

fn main() {
    let matrix = vec![
        vec![1, 2, 3],
        vec![4, 5, 6],
        vec![1, 2, 3],
        vec![7, 8, 9],
        vec![4, 5, 6],
        vec![0, 1, 2],
        vec![4, 5, 6],
    ];

    find_repeated_rows(&matrix);
}

  
   
/*
run:
   
Repeated Rows:
Row: [1,2,3] - Repeated 2 times
Row: [4,5,6] - Repeated 3 times
   
*/

 

 



answered May 24, 2025 by avibootz
...