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 trialMarko Rapaic
13,871 PointsIntermediate C# Challenge Task 3 of 3
So i've attempted this challenge with my code below. This successfully detects if an index item is preceded by a duplicate item, and then returns an appropriate bool value. You can see this in the stack trace output below.
When I initially got the System.IndexOutOfRangeException
I added the check in the if statement i != 0
so that if the index was 0, it wouldn't try looking outside of the array.
I've added some Console.WriteLine()
statements to show what's happening - you can see that it tries to go through the array twice for some reason, but crashes on the second attempt. I also tried discovering the length of sequence
and it came back with two seperate values....
Either way, not sure what the issue is, and would appreciate some fresh eyes :)
My code attempt:
namespace Treehouse.CodeChallenges
{
class RepeatDetector : SequenceDetector
{
public override bool Scan(int[] sequence)
{
bool isDuplicateFound = false;
foreach (int i in sequence)
{
System.Console.WriteLine(i);
if ((sequence[i] == sequence[i-1]) && (i != 0))
{
System.Console.WriteLine(i + " is a repeated value!");
isDuplicateFound = true;
}
}
System.Console.WriteLine("Duplicates found? " + isDuplicateFound + ".");
return isDuplicateFound;
}
}
}
Stack trace output:
1
2
3
3 is a repeated value!
3
3 is a repeated value!
4
Duplicates found? True.
1
2
3
4
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Treehouse.CodeChallenges.RepeatDetector.Scan (System.Int32[] sequence) <0x41bb71c0 + 0x0005d> in :0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /builddir/build/BUILD/mono-4.4.1/mcs/class/corlib/System.Reflection/MonoMethod.cs:295
--- End of inner exception stack trace ---
at Treehouse.CodeChallenges.Helpers.ReflectionHelpers.Call[T] (System.Reflection.MethodInfo method, System.Object instance, System.Object[] args) <0x41bb69d0 + 0x00027> in :0
at MonoTester.Run () [0x00130] in MonoTester.cs:104
at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:29
3 Answers
Marko Rapaic
13,871 PointsNevermind, got it working with the following:
namespace Treehouse.CodeChallenges
{
class RepeatDetector : SequenceDetector
{
public override bool Scan(int[] sequence)
{
// Begin initial for loop, assigning each index value to i as it loops.
for (int i = 0; i < sequence.Length; i++)
{
// Secondary for loop is used to add 1 to the previous for loop.
for (int j = i + 1; j < sequence.Length; j++)
{
// If the items at the same index in each array are the same, then a duplicate has been found.
if (sequence[i] == sequence[j]) return true;
}
}
return false;
}
}
}
Michele Morison
13,407 PointsThis worked for me in VS
namespace ConsoleApplication4 { 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; } } }
Chris Burbage
Courses Plus Student 4,693 Pointsnamespace Treehouse.CodeChallenges { class RepeatDetector : SequenceDetector { public override bool Scan(int[] sequence) { bool duplicateFound = false; for (int i=1; i< sequence.Length; i++) { if (sequence[i]==sequence[i-1]) duplicateFound = true; } return duplicateFound; } } }