1

Possible Duplicate:
Counting occurences of Javascript array elements

I have an array in javascript and for example the array is:

array(1,1,1,1,5,5,7,7);

If can some one please help me to understand how to count similar values, And how to join similar values, Thank you all and have a nice day.

Community
  • 1
  • 1
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
  • by _join similar values_, do you mean to _combine_ them into one unique value? – Michael Berkowski Jul 24 '12 at 18:24
  • It seems to be sorted already. Can't you image a simple algorithm to do what you want? Hint: use a loop. – Bergi Jul 24 '12 at 18:25
  • 3
    Try this: http://stackoverflow.com/questions/5667888/counting-occurences-of-javascript-array-elements – David Lojudice Sb. Jul 24 '12 at 18:27
  • 1
    it is better to show what you have tried or describe what you are thinking about doing. You will get much better responses that way – John Kane Jul 24 '12 at 18:31
  • 2
    If you don't understand something, do a little reading and try something out first. SO is better suited to help you with a specific question, with a little more [effort shown in the question](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Nick Jul 24 '12 at 18:33
  • Thank you all ill read the answers – Aviel Fedida Jul 24 '12 at 19:36

4 Answers4

2
var array = [1,1,1,1,5,5,7,7];

var count = 0;
var tempArray = array.sort();
var i;
var prevValue = null;
var joined = [];
for (i = 0; i < array.length; i++) {

    if (tempArray[i] != prevValue) {
        count++;
        prevValue = tempArray[i];
        joined.push(prevValue);
    }

}

​document.write(joined);​
Raman Zhylich
  • 3,537
  • 25
  • 23
1

I can suggest using underscore.js. The code you're looking for isn't hard to write, and would be a good learning experience. Once you've done that, underscore is fantastic convenience library that offers what you're looking for, and you don't have to maintain it. :)

The uniq function will give you a copy of your array without duplicates, and the size function will tell you how many values that contains (or just reference the .length property).

mwcz
  • 8,949
  • 10
  • 42
  • 63
  • 1
    Ah, yes, the "let's incorporate another library for every simple function we need to do" approach to web development. – Chris Sobolewski Jul 24 '12 at 18:31
  • 1
    That's what libraries are for. – mwcz Jul 24 '12 at 18:33
  • 2
    Suggesting a library is not an answer to a specific question as this. Unless OP asked how to do it with underscore, it is not on topic. –  Jul 24 '12 at 18:35
  • 1
    A +1. Because it isn't deserving of a -1. It answers how the problem can be solved in JS. Said library is still JS and the poster did not request "no libraries" .. although showing how it to solve in vanilla JS might be better for this posters level of experience :) –  Jul 24 '12 at 18:41
  • @pst: It's really too bad when people support off topic answers. Unless we know OP is using a library, such an answer is not helpful. By this standard, questions could be cluttered with dozens of answers that are unhelpful to the asker. Lest you forget, StackOverflow is not a discussion forum. Sadly, it's turning more and more into one –  Jul 24 '12 at 18:45
  • @pst ...an asker should not have to explicitly state that no library answer is desired. It's silly to put such a requirement on every question that simply wants a direct and on-topic answer to the question. –  Jul 24 '12 at 18:47
  • @amnotiam This discussion really belongs on meta, not SO. – mwcz Jul 24 '12 at 18:52
  • 1
    This is specific to your post. @pst upvoted it even though it makes no attempt to answer the question, and the second paragraph doesn't even help OP count the similar values as requested. –  Jul 24 '12 at 19:02
  • 1
    From the question: ***"...please help me to understand how to..."*** –  Jul 24 '12 at 19:07
  • @amnotiam Very good arguments. I would not have upvoted if not a -1. But still, the -1 seemed .. wrong somehow. –  Jul 24 '12 at 19:29
1

If you're looking to uniquely identify array contents:

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

  var myArray = [1,1,1,1,5,5,7,7];

  var uniquedMyArray = myArray.unique();
  var valueCountsMyArray = [];
  for (var i = 0; i < myArray.length; i++) {
     if (valueCountsMyArray[myArray[i]])
        valueCountsMyArray[myArray[i]]++;
     else
        valueCountsMyArray[myArray[i]] = 1;
  }
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
0

Here a solution to count how many time it contains a number.

var count = [];
for(var i=0; i<array.length; i++) {
    count[array[i]]++;
}
Matrix818181
  • 103
  • 1
  • 12