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
José Manuel Martínez López
18,133 PointsMethod or technic to know how many times a value is repeated in the array?
This is what I´ve done so far:
for var i = 0; i <= dConv.count - 1; i = i + 1{
if dConv[i] >= Intervalo1[0] && dConv[i] <= Intervalo2[0]{
aux.append(dConv[i])
aux2.append(Double(aux.count))
}
}
2 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI'm not sure if I'm understanding the problem properly, but I'm thinking you might be able to solve this by creating an array of counter variables that mirrors the array. (Or maybe this could be better implemented with some sort of object that has a value property and a counter property, and then you have an array of that object. I dunno.)
So you could try something like this:
for var i = 0; i < dConv.count; i++ {
if dConv[i] == Intervalo1[0] {
counterArray[0]++
}
}
Also FYI I cleaned up a few things in your code that I just think make it easier to read. I did i < dConv.count instead of i <= dConv.count - 1. I did i++ instead of i = i + 1. Also, to test if two values are equal, you can just use == instead of checking both <= and >=. Same results but I think it's cleaner code).
I'm also wondering if it would help you to have a loop within a loop so that you can test all the values of one array against all the values of another. Something like this:
for var i = 0; i < Intervalo1.count; i++ {
for var j = 0; j < dConv.count; j++ {
if Intervalo1[i] == dConv[j] {
counterArray[i]++
}
}
}
EDIT: Sorry for some reason the explanation I gave in the middle paragraph of the other minor edits I made to your code is getting messed up, whether I try and use backticks or just regular text the code and surrounding text is disappearing. Sorry.
José Manuel Martínez López
18,133 PointsHi Brendan, I'm getting this: Fatal error: Array index out of range
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI changed the code to be = dConv.count instead of less than or equal to dConv.count - 1. My explanation got messed up with some kind of forum bug sorry. It's the same result just cleaner code. Does that make sense?
José Manuel Martínez López
18,133 PointsHi Brenan, with the code you gave me I did this:
for var i = 0; i <= dConv.count; i = i + 1{
F.append(i)
}
for var i = 0; i < dConv.count; i = i + 1{
for var j = 0; j < Intervalo1.count; j = j + 1{
if dConv[i] >= Intervalo1[j] && dConv[i] <= Intervalo2[j]{
F[j]++
}
}
}
print(dConv)
print(Intervalo1)
print(Intervalo2)
for var i = 0; i < Intervalo1.count; i = i + 1{
print(F[i])
}
The first value is correct, but the others are not. Any idea what can be wrong?
Having this values [1.0, 3.0, 10.0, 67.0, 67.0, 78.0, 78.6, 100.0, 100.0, 378.0]
The correct number of values in the ranges given should be: 7, 2, 0, 0, 1
The limits are: Intervalo1[1.0, 95.0, 189.0, 283.0, 377.0] Intervalo2[95.0, 189.0, 283.0, 377.0, 378.0]
The values I´m getting are: 7, 3, 2, 3, 5
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWould you mind explaining to me - what is the purpose of the different arrays - F, dConv, Intervalo1, Intervalo2?.
Thanks.
José Manuel Martínez López
18,133 PointsI already figure out. Thanks for your help.
José Manuel Martínez López
18,133 PointsJosé Manuel Martínez López
18,133 PointsI only need the last value, but I can´t get it. Besides I need to store the times the value is repeated for the others array Intervalo position.