Skip to content

Commit 6041a2f

Browse files
authored
Add Duplicate Character Counter in V (#5197)
1 parent d7be67c commit 6041a2f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
fn show_error() {
4+
println('Usage: please provide a string')
5+
}
6+
7+
fn main() {
8+
args := os.args[1..] // skip program name
9+
if args.len == 0 {
10+
show_error()
11+
return
12+
}
13+
input_str := args[0]
14+
if input_str.len == 0 {
15+
show_error()
16+
return
17+
}
18+
19+
mut counts := map[rune]int{}
20+
for c in input_str.runes() {
21+
counts[c]++
22+
}
23+
24+
mut found := false
25+
for c in input_str.runes() {
26+
if counts[c] > 1 {
27+
println('${c.str()}: ${counts[c]}')
28+
counts[c] = 0 // prevent duplicate printing
29+
found = true
30+
}
31+
}
32+
33+
if !found {
34+
println('No duplicate characters')
35+
}
36+
}

0 commit comments

Comments
 (0)