Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# Polymorphism Virtual Methods

Getting results, but not passing the challenge

So I believe I have solved this, but perhaps just not the way the challenge needs me to. I did some testing by printing the entire array (to identify visually where the duplicates are), and "3" is a repeat. My code now prints the current index whose previous index matches the current index's value.

SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector
    {
        public virtual bool Scan(int[] sequence)
        {
            return true;
        }
    }
}
RepeatDetector.cs
namespace Treehouse.CodeChallenges
{
    class RepeatDetector : SequenceDetector
    {
        public override bool Scan(int[] sequence)
        {

            bool result = false;
            for (int index= 0; index <= sequence.Length -1; index++){
                if (index > 0 && sequence[index] == sequence[index-1])
                {
                    //System.Console.WriteLine(sequence[index]);
                   // System.Console.WriteLine(sequence[index -1]);
                    result = true;

                }
                else 
                {
                    result = false;
                }

            }
            return result;

        }
    }
}

1 Answer

Emmanuel C
Emmanuel C
10,636 Points

Hey Zach,

It seems that the problem is in the else part of that if statement. When youre looping and it detects that the current number is the same as the number preceding, it sets result to true. However it still loops again and the next number may not be the same as the number preceding it, so it sets result back to false. What actually gets returns is the what happened at the last iteration of the loop. If the duplicate number are in the middle of the sequence array but the last number is not the same as the number before it, it will return false, instead of true.

Deleting that else statement will fix this. The rest looks good.