Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

counts the duplicates in an array using for loop

function countDuplicates(arr) {
  const counts = {};
  for (let i = 0; i < arr.length; i++) {
    let element = arr[i];
    counts[element] = (counts[element] || 0) + 1;
  }
  return counts;
}
const arr = ["1", "5", "9", "14", "5", "22", "48", "25", "22", "20", "9", "13"];
console.log(countDuplicates(arr));
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery is element hidden 
Javascript :: react case switch not working 
Javascript :: copying object javascript 
Javascript :: get font size jquery 
Javascript :: react bootstrap add progress bar 
Javascript :: set _id to id 
Javascript :: js sum of int in array 
Javascript :: sequelize desc does not exist 
Javascript :: javascript join array 
Javascript :: useNavigate history back 
Javascript :: store input into array javascript 
Javascript :: style scoped vue 
Javascript :: nodejs bodyparser form data 
Javascript :: react focus textarea 
Javascript :: react native counter 
Javascript :: how to make back button react 
Javascript :: how to unban in discord js 
Javascript :: vuejs cant add script in template 
Javascript :: laravel http send data json raw 
Javascript :: state in constructor javascript 
Javascript :: javascript code to open excel file and read contents 
Javascript :: tick.json code 
Javascript :: vue js cdn 
Javascript :: javascript check for null variables 
Javascript :: how to concatenate strings and variables in javascript 
Javascript :: mongoose update push 
Javascript :: shorthand if statment in js 
Javascript :: string to number 
Javascript :: find a word in string javascript 
Javascript :: javascript sum table row values 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =