i tried using
use std::{iter::Inspect, str};
fn main() {
let mut Inp: String = String::from("jsjnkns");
let mut str: &str = &Inp;
let mut chr: Vec<char> = Inp.chars().collect::<Vec<_>>();
for mut x in chr.iter_mut(){
x.count==1;{
println!("numbers: {:?}", &x);
}
}
}
i want it to retun me "jsnk"
alice
October 27, 2021, 12:29pm
2
Please edit your post to follow our syntax highlighting guidelines:
To format code in this forum you need to surround the code with three backticks (```). For example, typing this...
```
fn main() {
println!()
}
```
...will be rendered as highlighted Rust code, like so:
fn main() {
println!()
}
You can also tell the forum software which language you're using by adding the language after the opening backticks.
```cpp
#include <iostream>
int main() {
std::cout << "Hello World!";
}
```
Will appear with highlighting as:
#include <iostream>
int main(…
alice
October 27, 2021, 12:31pm
3
use std::collections::HashSet;
fn remove_duplicates(mut s: String) -> String {
let mut seen = HashSet::new();
s.retain(|c| {
let is_first = !seen.contains(&c);
seen.insert(c);
is_first
});
s
}
1 Like
can you please quote this with an example string like "shdshsds"
alice
October 27, 2021, 12:34pm
5
I would be happy to help you further once you have fixed your original post.
alice
October 27, 2021, 12:38pm
7
You are using the wrong character. It should be a backtick or tilde character, not a period.
alice
October 27, 2021, 12:41pm
9
Alright, give this a try:
use std::collections::HashSet;
fn remove_duplicates(mut s: String) -> String {
let mut seen = HashSet::new();
s.retain(|c| {
let is_first = !seen.contains(&c);
seen.insert(c);
is_first
});
s
}
fn main() {
println!("{}", remove_duplicates(String::from("shdshsds")));
}
1 Like
yeah it did.. thanks alot
Hyeonu
October 27, 2021, 12:47pm
11
You can also exploit the return value of the HashSet::insert()
which is a bool.
fn remove_duplicates(mut s: String) -> String {
let mut seen = HashSet::new();
s.retain(|c| seen.insert(c));
s
}
7 Likes
system
Closed
January 25, 2022, 12:48pm
12
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.