2

New to programming and I was just practicing this Question. Return the longest sequence of characters in an array. My answer came out to be

public void rLong()
{
    string sequence = "aabcccddeeee";

    char mac='\'';
    char[] sArray = new char[8];
    sArray = sequence.ToCharArray();

    int k;
    int tmp=0;
    int i;

    for(i=0; i < sArray.Length-1; i++)
    {

        int count=0;
        for(k=0; k < sArray.Length-1; k++)
        {
            if(sArray[i]==sArray[k])
            count++;

            if(count>=tmp)
            { 
            tmp=count;  
            mac=sArray[i];      
            }
        }
    }
    Console.WriteLine("highest letter is {0} and count is {1}", mac,tmp);
}

Answer: highest letter is e and count is 3

This answer gave me the right char(e) but the wrong count(3) it should be 4. But through trial and error I figured if i take the -1 out on the sArray.Length in the inner for loop I get the right count(4).

Can someone explain why? I thought always to put -1 on array.length when looping through its index. Is it different when using a nested for loop? Any help would be appreciated. Thank You

0

2 Answers 2

4

If you want your for loop to iterate over all of the elements then you need to use i < sArray.Length.

In your string, there are 12 chars, which means we want to loop over it 12 times. The loop starts at 0 and ends when i < sArray.Length is no longer satisfied, i.e. i = 12. This means i will go from 0 to 11, which is 12 total iterations. If you did i < sArray.Length - 1 then it would stop at i = 11, which is 11 iterations.

This is the exact same situation with your inner for loop using k, you want to iterate over the whole string again, so it needs to use k < sArray.Length without the -1.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you should mention both loops in your answer i.e i and k
@FaizanRabbani Good point, OP might not realize this applies to both loops.
1

That code has some problems

One you are not iterating the whole array. Should be k < sArray.Length;. On a zero based array you need to go from 0 to Length - 1. If you used <= then you could use Length - 1.

You are not finding a sequence. You are counting total number of matches and not even doing that very efficiently.

Very limited test case. You should have cases where the the character is split.
e.g. aaabbbcccaaffff

You got lucky the last sequence was the longest to catch that error.

This does it in one pass.

static void rLong()
{
    string sequence = "aabcccddaaaeeee";

    char? lastChar = null;
    int longestSequence = 0;
    int currentSequence = 1;
    char? longestChar = null;

    foreach (char c in sequence)
    {
        if (lastChar != null)
        {
            if (c == lastChar)
            {
                currentSequence++;
                if(currentSequence > longestSequence)
                {
                    longestSequence = currentSequence;
                    longestChar = c;
                }
            }
            else
            {
                currentSequence = 1;
            }
        }
        lastChar = c;
    }
    Debug.WriteLine("highest letter is {0} and count is {1}", longestChar, longestSequence);
} 

1 Comment

You can give it the check if it is the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.