Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to find out most repeated string in an array js

var cats = ['Tom','Fluffy','Tom','Bella','Chloe','Tom','Chloe'];
var counts = {};
var compare = 0;
var mostFrequent;
(function(array){
   for(var i = 0, len = array.length; i < len; i++){
       var word = array[i];
       
       if(counts[word] === undefined){
           counts[word] = 1;
       }else{
           counts[word] = counts[word] + 1;
       }
       if(counts[word] > compare){
             compare = counts[word];
             mostFrequent = cats[i];
       }
    }
  return mostFrequent;
})(cats);
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery find tag and class 
Javascript :: regex quantifiers 
Javascript :: 2nd highest number from array 
Javascript :: check box jquery 
Javascript :: hypot in javascript 
Javascript :: add class with jquery 
Javascript :: how to check string uppercase or lowersace using regex javascript 
Javascript :: javascript string to number 
Javascript :: mongoose express js require 
Javascript :: change js 
Javascript :: window change detect 
Javascript :: document.queryselectorall extract all href element 
Javascript :: react native toast message 
Javascript :: how to pass data in body of delete request angular 
Javascript :: create document mongoose 
Javascript :: summation js 
Javascript :: react onclick runs on load 
Javascript :: clean collection mongoose 
Javascript :: jquery each hover 
Javascript :: arry to object using reduce 
Javascript :: multiple checkbox validation in jquery 
Javascript :: loop over an array 
Javascript :: convert timestamp to utc javascript 
Javascript :: lodash update object by id in array 
Javascript :: format date in javascript 
Javascript :: update data using mongoose 
Javascript :: declare function javascript 
Javascript :: jest run specific test 
Javascript :: how to compare arrays in js 
Javascript :: vue change specific params/query 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =