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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Jose Badillo](https://uploads.teamtreehouse.com/production/profile-photos/9352042/micro_WP_20171016_19_08_59_Pro.jpg)
Jose Badillo
1,186 Pointsneed help with this override method challenge iv already tried finding answers else where help please
so okay I don't know if you guys can see what I alredy did here but I'm not getting any compiler error just that the index is out of bounds but how can I stop this from happning like I don't know how I could solve this only thing I can think of is to create a nother loop but then that just would brake the whole thing I really don't know where I'm going wrong so this is why I don't know how to solve it
namespace Treehouse.CodeChallenges
{
class SequenceDetector
{
public virtual bool Scan(int[] sequence)
{
return true;
}
}
}
namespace Treehouse.CodeChallenges
{
class RepeatDetector : SequenceDetector
{
public override bool Scan(int[] sequence)
{
int seqCompare = 0;
foreach(int i in sequence)
{
bool result = sequence[i] == seqCompare;
if(result)
{
return true;
}
seqCompare = sequence[i];
}
return false;
}
}
}
/*namespace Treehouse.CodeChallenges
{
class RepeatDetector : SequenceDetector
{
private bool z;
new public virtual bool Scan(int[] sequence)
{
for (int i = 1; i < sequence.Length; i++)
{
if (sequence[i - 1] == sequence[i])
{
z = true
}
return z;
}
}
}
}*/
1 Answer
![Simon Coates](https://secure.gravatar.com/avatar/9c452f3f160ef69f85f1ff042db63980?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Simon Coates
28,694 PointsIt's been a while since i did this exercise, but it seemed to accept
namespace Treehouse.CodeChallenges
{
class RepeatDetector: SequenceDetector
{
public override bool Scan(int[] sequence)
{
int? prev = null;
foreach(int x in sequence)
{
if(prev == x)
{
return true;
}
prev = x;
}
return false;
}
}
}
the ? after int makes prev nullable.
The following also seemed to pass:
namespace Treehouse.CodeChallenges
{
class RepeatDetector: SequenceDetector
{
public override bool Scan(int[] sequence)
{
int prev = 0;
int iteration = 0;
foreach(int x in sequence)
{
iteration++;
if(iteration > 1 )
{
if(x == prev)
{
return true;
}
}
prev = x;
}
return false;
}
}
}
![Jose Badillo](https://uploads.teamtreehouse.com/production/profile-photos/9352042/micro_WP_20171016_19_08_59_Pro.jpg)
Jose Badillo
1,186 PointsThanks Simon I appreciate the feed back I will take detail notes on this way to approach the problem. The solutions you came up with are brilliant and I'm really putting all my effort to learn as much as I can so knowing that there is a simpler way to solve this is absolutely and plus. Thanks again.
Jose Badillo
1,186 PointsJose Badillo
1,186 PointsOkay so I figured out a way to get around it before anyone had a chance to reply. Just wanted to update you guys but please if you do have a different solution than mine do share it because I feel like there should be a simpler solution than the one I came up with. Okay so my solution to this issue was to take the for loo[ and wrap it in a try catch statement and catch any System.Exception being return and upon an exception being thrown I return false meaning that one the loop checked every value in the index for repetitions it would go out of bounds and that means that in the array there are no repetitions so upon going out of bounds the exception is thrown and I return false and if the loop dose catch the repetition it will return true there and then and that is that. But I feel there should be a way to check the index inside the actual loop and still check the repetitions all in more simple code and I really am not that far in this course so if there are simpler way and you would like to share then please feel free and thank you Community, for always helping to find solutions when we get stuck somewhere it really is helpful :)