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

Jose Perez
Jose Perez
1,338 Points

I need help understanding the answer for this challenge Please!

I know I have the correct answer but what happens if the following happens? If we have an array as follow: [1, 2, 3, 1, 5, 6] if we start a index 1 and compare to index 0 and then increase every time. What will happen reaching index 3 and compare to index 2, it will not catch the 1 in index 0 since it will once compare to the one before it? can you please help understand this?

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)
        {  
              for(int i=1; i < sequence.Length ; i++)
              { 
                   if(sequence[i] == sequence[i - 1])
                   {
                        return true;
                   }
              }
              return false;
        }
   }
}

2 Answers

andren
andren
28,558 Points

What will happen reaching index 3 and compare to index 2, it will not catch the 1 in index 0 since it will once compare to the one before it?

You are correct, it won't detect index 0 as a repeat but that is the intended behavior.

The challenge states that the Scan method should only return true if: "any value in the array is a repeat of the value immediately preceding it."

Meaning that only sequences where two identical numbers appears in a row should be classified as a repeat, duplicate numbers that are not right next to each other should not be counted as a repeat.

That means that a sequence like this [1, 3, 5, 1, 3] should not be classified as repeating, but a sequence like this [1, 3, 3, 8, 5] should be. And the function you have written follow those requirements correctly.

Jose Perez
Jose Perez
1,338 Points

Oh ok than that makes more sense. thanks for your helpful response!