C
C#2y ago
Caioon9

✅ Repeated Letters

Hi, I have a question, how do I do this in C#, I have no idea, I've tried many ways
35 Replies
Jimmacle
Jimmacle2y ago
what are some of the ways you've tried?
Caioon9
Caioon9OP2y ago
everything i saw on overflow, youtube
Caioon9
Caioon9OP2y ago
Caioon9
Caioon9OP2y ago
Smadge and this
Jimmacle
Jimmacle2y ago
dear lord what is that
Caioon9
Caioon9OP2y ago
i said i tried harold
Jimmacle
Jimmacle2y ago
so, first thing is you never need to actually care which letter it is, only if it's the same as the one before it
Caioon9
Caioon9OP2y ago
ok
Shinigami
Shinigami2y ago
Maybe you can do this Sort each word alphabetically and search of there are 2 same letters one after another
Cattywampus
Cattywampus2y ago
@Caioon9 split the string first via string.Split(' ') then do nested loop on it the outer loop is your splitted string and the two inner loops will be comparing each character so the tree should look like this
for(A)
for(A.A)
for(A.B)
for(A)
for(A.A)
for(A.B)
we can do this with linq quite easily but bcos you're just started with the language lets just do it manually
basically, i am little cat
WHAT IS THIS
Cattywampus
Cattywampus2y ago
he's learning the language, saying that would not help him solve the question
Cattywampus
Cattywampus2y ago
it's a fun question, you should try to solve it
Shinigami
Shinigami2y ago
Do we have a DSA related channel?
Cattywampus
Cattywampus2y ago
no response, anyway here
Shinigami
Shinigami2y ago
Gg the most brute force wat Way*
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
string str = "Today is the greatest day ever";
var split = str.Split(' ');
int index = -1;
int counter = 0;

for(int i = 0; i < split.Length; i++)
{
int found = 0;

for(int a = 0; a < split[i].Length; a++)
{
for(int j = 0; j < split[i].Length; j++)
{
if(split[i][a] == split[i][j] && a != j)
found++;

if(found >= counter)
{
counter = found;
index = i;
}
}
}
}

string foundWord = index > -1 ? split[index] : index.ToString();
Console.WriteLine("Most repeated char is : " + foundWord);
string str = "Today is the greatest day ever";
var split = str.Split(' ');
int index = -1;
int counter = 0;

for(int i = 0; i < split.Length; i++)
{
int found = 0;

for(int a = 0; a < split[i].Length; a++)
{
for(int j = 0; j < split[i].Length; j++)
{
if(split[i][a] == split[i][j] && a != j)
found++;

if(found >= counter)
{
counter = found;
index = i;
}
}
}
}

string foundWord = index > -1 ? split[index] : index.ToString();
Console.WriteLine("Most repeated char is : " + foundWord);
Console Output
Most repeated char is : greatest
Most repeated char is : greatest
Compile: 329.912ms | Execution: 60.766ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus2y ago
lmao 😽 if you're confused with how it works, let me know
Florian Voß
Florian Voß2y ago
even if we wanna avoid linq and also brute force it I can't leave a triple nested loop here as the solution. It is not needed and should be avoided instead of doing for for for try to solve doing only for for
Cattywampus
Cattywampus2y ago
sure, feel free to post your answer Also to be clear, linq shouldn't be avoided if you already grasp the fundamentals.. in this case, it is clear that they're new to the language
Florian Voß
Florian Voß2y ago
I'd rather let him try to get it but if you ask for it. Since we wanna avoid using Linq i go based of your solution and just remove the most inner loop. Here is some pseudo code, I dont have an IDE installed
//create dictionary to store the char as key and how often we have seen it as value
foreach(var word in split){

foreach(var char in word){
//if dict contrains char, increase value
//otherwise add char to dict
}
}
//return based of your highest value in dictionary
//create dictionary to store the char as key and how often we have seen it as value
foreach(var word in split){

foreach(var char in word){
//if dict contrains char, increase value
//otherwise add char to dict
}
}
//return based of your highest value in dictionary
Cattywampus
Cattywampus2y ago
you can do it here !eval command or go to https://dotnetfiddle.net/ and do it there and paste the link here
Caioon9
Caioon9OP2y ago
lmao i am Smadge
Florian Voß
Florian Voß2y ago
@BadaBingBadaBoom https://dotnetfiddle.net/rKMKWl here is how I implemented it without using Linq. Sorry for late response. I haven't tested it much but It should be working tho I just switched ever and greatest around in the input and checked if that would change my output and it did
Cattywampus
Cattywampus2y ago
that's incorrect it shouldn't care about the order
Florian Voß
Florian Voß2y ago
yes it should it says it should give the first word having the most same letter in it
Cattywampus
Cattywampus2y ago
ever isn't the most same letter
Florian Voß
Florian Voß2y ago
the first word with greatest number of repeated letters
since both greatest and ever only have 2 when we swap those in input that changes output its two e's in ever. greatest has two e's and two t's so both have two as their maximum of repeated letters. so we return the first word of those two thats how I understood the task and solved it and it works under those requirements ^^ but would be easy to adjust I'm pretty sure I got the task right and you missunderstood it. Read the text again, it counts two e's and two t's but never adds them up and counts them as 4 for greatest and if you were to write it with linq grouping on the character, ordering by their count then you would have the same behaviour
Cattywampus
Cattywampus2y ago
AABBCC -> AABBCCDD -> AABB :: which one of those 3 is the 1st greatest with repeating letters?
Florian Voß
Florian Voß2y ago
the first entry all have two as their maximum but the first entry is the first word having the most (2) amount of repeated letters @BadaBingBadaBoom
Caioon9
Caioon9OP2y ago
the output has to be the first word with repeated letter
Florian Voß
Florian Voß2y ago
can you send me a link to that task? do they have tests on that website?
Caioon9
Caioon9OP2y ago
it's a closed test, I don't think there's any way to send it but I will try
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.

Did you find this page helpful?