Skip to content

Commit b96c0fd

Browse files
gangaasoonupascalecu
authored andcommitted
Add Duplicate Character Counter in Dart (TheRenegadeCoder#4980)
1 parent 83dbc4a commit b96c0fd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Issue 4974
2+
void main(List<String> args){
3+
const String error_message = "Usage: please provide a string";
4+
if (args.isEmpty || args[0].isEmpty){
5+
print(error_message);
6+
return;
7+
} // end of empty check
8+
9+
Map<String, int> frequency = {};
10+
bool duplicate_not_found = true;
11+
12+
for (int i = 0; i < args[0].length; i ++){
13+
String char = args[0][i];
14+
if (RegExp(r'[a-zA-Z0-9]').hasMatch(char)) {
15+
frequency[char] = (frequency[char] ?? 0) + 1;
16+
}
17+
} // end of checking duplicate characters
18+
19+
//either print all duplicate letters or set flag duplicate letters not found
20+
frequency.forEach((char, count){
21+
if (count > 1) {
22+
duplicate_not_found = false; // at least 1 duplicate letter is found
23+
print('$char: $count');
24+
}
25+
});
26+
// print if no duplicate characters
27+
if(duplicate_not_found == true){
28+
print("No duplicate characters");
29+
}
30+
}

0 commit comments

Comments
 (0)