C
C#15mo 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
Jimmacle15mo ago
what are some of the ways you've tried?
Caioon9
Caioon915mo ago
everything i saw on overflow, youtube
Caioon9
Caioon915mo ago
Caioon9
Caioon915mo ago
Smadge and this
Jimmacle
Jimmacle15mo ago
dear lord what is that
Caioon9
Caioon915mo ago
i said i tried harold
Jimmacle
Jimmacle15mo 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
Caioon915mo ago
ok
Shinigami
Shinigami15mo ago
Maybe you can do this Sort each word alphabetically and search of there are 2 same letters one after another
Cattywampus
Cattywampus15mo 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
Cattywampus15mo ago
he's learning the language, saying that would not help him solve the question
Cattywampus
Cattywampus15mo ago
it's a fun question, you should try to solve it
Shinigami
Shinigami15mo ago
Do we have a DSA related channel?
Cattywampus
Cattywampus15mo ago
no response, anyway here
Shinigami
Shinigami15mo ago
Gg the most brute force wat Way*
MODiX
MODiX15mo 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
Cattywampus15mo ago
lmao 😽 if you're confused with how it works, let me know
Florian Voß
Florian Voß15mo 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
Cattywampus15mo 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ß15mo 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
Cattywampus15mo ago
you can do it here !eval command or go to https://dotnetfiddle.net/ and do it there and paste the link here
Caioon9
Caioon915mo ago
lmao i am Smadge
Florian Voß
Florian Voß15mo 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
Cattywampus15mo ago
that's incorrect it shouldn't care about the order
Florian Voß
Florian Voß15mo ago
yes it should it says it should give the first word having the most same letter in it
Cattywampus
Cattywampus15mo ago
ever isn't the most same letter
Florian Voß
Florian Voß15mo 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
Cattywampus15mo ago
AABBCC -> AABBCCDD -> AABB :: which one of those 3 is the 1st greatest with repeating letters?
Florian Voß
Florian Voß15mo 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
Caioon915mo ago
the output has to be the first word with repeated letter
Florian Voß
Florian Voß15mo ago
can you send me a link to that task? do they have tests on that website?
Caioon9
Caioon915mo ago
it's a closed test, I don't think there's any way to send it but I will try
Accord
Accord15mo 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
More Posts
❔ what is difference between _ and ()?I have following in constuctor: ```cpp var initialize = ReactiveCommand.CreateFromTask( async () =>❔ Updating EF entity with object containing optional propertiesI'm writing an API endpoint where you can pass in any number of parameters for an EF core entity. I ❔ Properties.Settings.Default producing different stored values when called from different winFormsEnvironment (Autodesk Revit API) I am using `Properties.Settings.Default` and can update, reload or❔ HTTPclient fails to POST properlyTarget website has a very basic login form, and I have no problems logging in via curl: ``` curl1 -v❔ Returning the user object that was just created after web api [POST]I have a web API and the POST works just fine in Swagger. It does work fine if I use the web API fro❔ How to get file path of PDFs in project.Hello! I am facing an issue with my current project. I want to be able to get a combobox in my Winfo❔ Running ASP.NET file in RiderNot sure why i cant seem to run the file - I'd like to do some testing on this work in progress proj❔ ASP.NET Core Web API controller method parameter is not null, but property values are always null.I have a method on a controller that takes a model class as a parameter. Calling the method from an❔ change timezone of aspnet docker containerhow to change aspnet docker container's timezone? i am using this image `mcr.microsoft.com/dotnet/as❔ Processes and AsyncHow can I recreate this with Async? (BeginOutputReads and such) ```cs // If an error happens, prints