6

I have a string

var stringIHave = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$";

How to get the count of the number of occurrences of each entry, The occurrence I get, is from a JSON like Java = 8 and etc...

Chetan
  • 1,590
  • 3
  • 19
  • 34

6 Answers6

6

First of all you need to split your srting to array:

var keywordsArr = stringIHave.split( '$$' );

then you need to have an object for example to store counts:

var occur = {};

and then just create simple for loop to count all occurrences:

for( var i = 0; i < keywordsArr.length; i++ ) {
    occur[ keywordsArr[ i ] ] = ( occur[ keywordsArr[ i ] ] || 0 ) + 1;
}

now your object occur will have names as keys and count as values.

See jsFiddle demo.

Also as you have at end of your string $$ you maybe will need to remove last item from keywordsArr so just do after split function call:

keywordsArr.pop();

See demo without last element.

So final code will be like:

var stringIHave = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$",
  keywordsArr = stringIHave.split( '$$' ),
  occur = {};

keywordsArr.pop();

for( var i = 0; i < keywordsArr.length; i++ ) {
    occur[ keywordsArr[ i ] ] = ( occur[ keywordsArr[ i ] ] || 0 ) + 1;
}

for( var key in occur ) {
    document.write( key + ' - ' + occur[key] + '<br/>' );        
} ​
antyrat
  • 27,479
  • 9
  • 75
  • 76
  • This was the only answer that helped me get the count or duplicates after a frustrating last 6hrs. I have an array of strings which I had to concatenate together and then using your logic was able to find out the duplicate words in the complete string. Am sure it is probably too much overhead but I for now am just relieved to get a solution. – Sid Feb 10 '17 at 12:24
5

I'd suggest the following:

function stringCount(haystack, needle) {
    if (!needle || !haystack) {
        return false;
    }
    else {
        var words = haystack.split(needle),
            count = {};
        for (var i = 0, len = words.length; i < len; i++) {
            if (count.hasOwnProperty(words[i])) {
                count[words[i]] = parseInt(count[words[i]], 10) + 1;
            }
            else {
                count[words[i]] = 1;
            }
        }
        return count;
    }
}

console.log(stringCount("Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$", '$$'));
​

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
3

It's not entirely clear what final objective is. Following creates an object from string that looks like

Object created:

{
 "Java": 8,
 "jQuery": 4,
 "Hibernate": 1,
 "Spring": 1,
 "Instagram": 1
}

JS:

var str = 'Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$';
var arr = str.split('$$')
var obj = {};

for (i = 0; i < arr.length; i++) {
    if (arr[i] != '') {
        if (!obj[arr[i]]) {
            obj[arr[i]] = 0;
        }
        obj[arr[i]]++;

    }
}

You can loop over the object to get all values or simply look up one value

var jQueryOccurences= obj['jQuery'];

DEMO: http://jsfiddle.net/25hBV/1/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

Split the string into an array, and putting the array into an object takes care of duplicates and counts occurences as key/value pairs in the object, see fiddle!

var stringIHave = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$",
    s = stringIHave.split('$$');
    obj = {};

for (var i=s.length; i--;) {
    obj[s[i]] = (s[i] in obj) ? obj[s[i]]+1 : 1;
}

//    obj.Java == 8

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

If you want it short and sweet:

// variable declarations
var arParts = stringIHave.match(/\w+/g), 
    result = {}, 
    i = 0, 
    item;

// Copy the array to result object      
while (item = arParts[i++]) result[item] = (result[item] || 0 ) + 1;

demo

closure
  • 7,412
  • 1
  • 23
  • 23
1

Now a days you can do

    const str = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$";
    
    var result = str.split("$$").reduce(function(acc, curr) {
    curr &&  (acc[curr] = (acc[curr] + 1) || 1);
     return acc
    }, {});
    console.log(result);
Pawan Singh
  • 824
  • 6
  • 13