© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
9 replies
Mitsu

Help with optimisation

Hey guys:
Have this code here for the following requirements :
//“Have the function MyLongestString(string[] strings) take an array of strings and return the word that is the longest in length. If there are two or more words with equal length then return the first word. If the array is empty return the string -1”.


        public (int length, string LongestString) MyLongestStringValueTuple(string[] strings){
        if(strings.Length == 0){
            return (-1, string.Empty);
        }
        string longest = strings[0];
        foreach(string str in strings){
            if(str.Length > longest.Length){
                if(str.Length == longest.Length){
                    continue;
                }
                longest = str;
            }
        }
        return (longest.Length, longest);
        public (int length, string LongestString) MyLongestStringValueTuple(string[] strings){
        if(strings.Length == 0){
            return (-1, string.Empty);
        }
        string longest = strings[0];
        foreach(string str in strings){
            if(str.Length > longest.Length){
                if(str.Length == longest.Length){
                    continue;
                }
                longest = str;
            }
        }
        return (longest.Length, longest);


I really don't like the double if statements I was thinking of using a HashMap but unsure on how to construct it, I mean this is fine but i want to improve. any advice would be great
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Pacman-esque game in c# raylib. Help with optimisation/collision
C#CC# / help
3y ago
Help with ReportViewer
C#CC# / help
12mo ago
Help with error
C#CC# / help
17mo ago
✅ help with assignment
C#CC# / help
2y ago