C
C#2y ago
supriseX

❔ error CS1525: Unexpected symbol `String'

using System; public String DuplicateCount(string input) {
foreach (char a in input) { int i = a.Count; if (i > 1){ Console.WriteLine($"{a}:{i}");} }
} DuplicateCount("indivisibilities"); why doesn't this work?
13 Replies
leowest
leowest2y ago
a.Count is invalid
supriseX
supriseXOP2y ago
yes, but that's not the main problem here somehow String on line 3 isn't recognised
leowest
leowest2y ago
what is line 3
supriseX
supriseXOP2y ago
public String DuplicateCount(string input)
leowest
leowest2y ago
well the other error is that your method does not have a return
supriseX
supriseXOP2y ago
good point, im new to this so i forgot
leowest
leowest2y ago
if you were using visual studio, it would have pointed those out for u what IDE are you using
supriseX
supriseXOP2y ago
online, vs code is being weird, doesn't wanna run.
leowest
leowest2y ago
ah well I suggest u use visual studio community instead its really helpful while learning so what are you trying to do here
foreach (char a in input)
{
int i = a.Count;
if (i > 1){
Console.WriteLine($"{a}:{i}");}
}
foreach (char a in input)
{
int i = a.Count;
if (i > 1){
Console.WriteLine($"{a}:{i}");}
}
are u trying to skip the first letter?
MODiX
MODiX2y ago
Servator#3472
REPL Result: Success
var input = "indivisibilities";

var groups = input.GroupBy(c => c).Where(group => group.Count() > 1);

foreach (var group in groups)
{
Console.WriteLine($"{group.Key} {group.Count()}");
}
var input = "indivisibilities";

var groups = input.GroupBy(c => c).Where(group => group.Count() > 1);

foreach (var group in groups)
{
Console.WriteLine($"{group.Key} {group.Count()}");
}
Console Output
i 7
s 2
i 7
s 2
Compile: 810.352ms | Execution: 103.783ms | React with ❌ to remove this embed.
leowest
leowest2y ago
ah yeah silly me should have read the method name so yeah what you want to do is, you might want to have a Dictionary to hold the char and count as you iterate in your loop and add to the dictionary and then loop thru the dictionary where your Value is bigger than one a is a char so you can't use Count like that which is why its invalid
supriseX
supriseXOP2y ago
oh this is what i am trying to do tho thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server