3

I have an array of n number of items

var arr1 = [2, 0, 0, 1, 1, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 1, 0, 1, 1, 0, 2, 1, 0, 0, 0, 2, 1, 1, 1, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 0, 2, 2, 0, 2, 0, 0, 1, 2, 1, 0, 2, 1, 0, 1, 2, 0, 2, 0, 0, 0, 1, 2, 1, 0, 2, 0, 0, 0, 1, 2, 1, 1, 1, 1]

as you see the array only has i different values (v) (0,1,2),i = 3 in that case

What I would like is ending up with an array like this one.

var arr2 = [23, 45, 64]

the length of the arr2 array should corresponds to i and the values sould be the occurences of each value(v)

I am doing all kinds of loops and conditionals, but looking for a straight solution. my part so far http://jsfiddle.net/fiddlebjoern/aSsjy/2/

jQuery and/or underscore may be involved.

1

4 Answers 4

8

Easy peasy! The values of your input become the keys of your output; you accumulate on those keys as you encounter the values:

var arr1 = [0,0,0,1,2,3,4,3,2,3,4,3,4,3,5];
var arr2 = [];

for (var i = 0; i < arr1.length; i++) {
   var n = arr1[i];
   if (arr2[n] != undefined)
       arr2[n]++;
   else
       arr2[n] = 1;
}

console.log(arr2);  // Output: [3, 1, 2, 5, 3, 1]

Live demo.

Sign up to request clarification or add additional context in comments.

8 Comments

Close, but no cigar. The members of the array must be initialised as numbers (zero would be best). What you have returns [,NaN,NaN,NaN,NaN,NaN].
@RobG: I did. And, no, it doesn't. Did you click on the link labelled "demo"? Or even read the code?
No, I pasted your original posted code into a browser. You've changed it so it works.
really good ... i didnt mention it, but there could also be a string in the initial array.
@RobG: About 15 seconds after posting, yes. That was 16 minutes before your comment.
|
5

Sounds like a job for "reduce"

arr2 = arr1.reduce(function(a, v) {
    a[v] = (a[v] || 0) + 1;
    return a;
}, [])

Also, your arr2 is actually associative, so it's better to use an object instead:

map = arr1.reduce(function(a, v) {
    a[v] = (a[v] || 0) + 1;
    return a;
}, {})

reduce is Javascript 1.8, for older browsers you need an emulation or use a library like underscore.js.

Example with underscore.js:

arr2 = _(arr1).chain().groupBy(_.identity).map(_.size).value()

This is perhaps not so "easy" as other answers, but at least you can learn something from this.

For the sake of being complete, here's the correct way to use a plain loop for the same task:

counter = [] // or {}
for (var i = 0; i < arr.length; i++)
     counter[arr[i]] = (counter[arr[i]] || 0) + 1;

3 Comments

His code explicitly states he wants an array, not a normal object.
@LightnessRacesinOrbit—which is likely why an answer with an array was given first, but an object suggested as a possibly more suitable option.
@RobG: It's given as part of a logical inference ("so") predicated on "your arr2 is actually associative", which makes little sense when an explicit requirement was that it should no longer be. Still, apparently the question has since changed.
2

Based on the other answer, here's one that works:

var arr1 = [1,2,3,4,3,2,3,4,3,4,3,5];
var arr2 = [];
var n, m;

for (var i=0, iLen=arr1.length; i < iLen; i++) {
  n = arr1[i];
  m = arr2[n];
  arr2[n] = m? ++m : 1;
}

alert(arr2)

2 Comments

It would be more readable to use || instead of the ternary operator: arr2[n] = (arr2[n] || 0) + 1
For some, whichever way suits. More to type though. :-)
1

You can use array_count_values:

<?php
     $array = array(1, "hello", 1, "world", "hello");
     print_r(array_count_values($array));
?>

The output is:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

2 Comments

And use which javascript php live interpreter?
Congratulations on not bothering to read the question whatsoever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.