Q: Javascript Arrays
- Started
- Last post
- 17 Responses
- OZute
I have an array containing 120.000 integers varying from 1 to 6. Now I like to count the integers with e.g. number 1 within the array.
The result should be something like 20.000 integers equal to number 1.
I haven’t got a clue how to do this. I can count the total amount of integers within the array, so it might be an option to split the array into 6 new arrays all containing similar integers.Is there an easy way to count a range of numbers within an array?
- rabattski0
not a javascript buff but a php buff but you could do a loop where the total loop equals the total length of the array and check every integer if it matches e.g. 1 and if so e.g. total++. but with 120000 integers that's gonna be slow as hell. splitting it in 6 arrays and counting the total length is def. an option and might be faster as well. but you lose the position of the original though.
- shaft0
I hope i understood what you meant..
(arr is your array)var count=0;
var integ=6; //your integer
for (i=0;i
- shaft0
shit it was cut in posting, have to start over again
- rabattski0
that's what i said. don't you think it's gonna be really slow if the array length is 120000?
- shaft0
I can't think of other algorithm, but maybe a for..in loop would be faster.
- OZute0
Thanks for the help! It might be slow but for 120.000 integers it's ok. I tried to do it with 1.2 mln integers as well. But then a get a warning message that my system may become unstable.
Anyway, splitting the array is at least as slow as counting all the integers. Because my computer has to determain for every integer in wich new array he has to put it.
- shaft0
This is fast for me:
http://biggermedia.com/nt/counte…
The script generates more or less random numbers, writes 'array ready' and counts 6's.
The time between 'array ready' and displaying number of 6's is really short
- rabattski0
well in that case go with the suggested loop.
- shaft0
well in that case go with the suggested loop.
rabattski
(Dec 29 04, 04:18)
-----------------------
The man in a greenish yellow car, please stay calm and use the suggested loop. Do not use the loop that wasn't suggested to you. I repeat, Stay away from the loop that wasn't suggested to you.
- rabattski0
lmao! :D
- E__________0
if you are worried about your script slowing down a client this will make it faster:
var n=0;
my_array.sort();
while(my_array[n] == 1)
{
my_array2.push(my_array[n]);
n++;
}
obviously 1 should be the lowest value, if not (0 is the lowest) use a for loop and break when the number is not 1 anymore.
- shaft0
Wait a second.. why would a sober person want an array of 20 000 same numbers?
- jkosoy0
s'what I was thinking shaft. but the only logical answer to that is that he's drunk. :)
- enobrev0
all drunkeness aside, what do you need 120k duplicate values in a n array for?
- OZute0
Well it has something to do with dice and casinos.
- stewart0
OZute-online-gambling.com?