Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

number of repetition in an array javascript

let targetted_array = [3,4,3,23,452,5,3,3,21,1,5,5];  

function countInArray(array, element) {
    let repeat_count = 0;
    for (let i = 0; i < array.length; i++) {
        if (array[i] === element) {
            repeat_count++;
        }
    }
    return repeat_count;
}

countInArray(targetted_array, 3) // will return 4. cuz "3" was repeated 4 times in the targetted_array
countInArray(targetted_array, 5) // will return 3. cuz "5" was repeated 3 times in the targetted_array
Comment

js find duplicates in array and count of numbers

const arr = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"]
const counts = {};
arr.forEach((x) => {
  counts[x] = (counts[x] || 0) + 1;
});
console.log(counts)
Comment

PREVIOUS NEXT
Code Example
Javascript :: duplicate value removed in array of object in javascript 
Javascript :: javascript tolocaletimestring 
Javascript :: url regex javascript 
Javascript :: async for loop 
Javascript :: post data from api using jquery ajax 
Javascript :: convert string to number javascript 
Javascript :: File line by line reader Node js 
Javascript :: jquery select2 how to make dont close after select 
Javascript :: react conditional styling 
Javascript :: js find array return true false 
Javascript :: if datatable is null js 
Javascript :: axios.defaults.withCredentials = true 
Javascript :: sort object dictionary javscript 
Javascript :: how to remove all child elements in javascript 
Javascript :: mongoose unique error message 
Javascript :: angularjs accordion access toggle 
Javascript :: Nullish Coalescing Vs Logical OR opreators 
Javascript :: js generate random numbers 
Javascript :: server status minecraft javascript 
Javascript :: how to disable copy paste in input js 
Javascript :: truncate a string js 
Javascript :: how to read 2 dimensional array in javascript 
Javascript :: dictionary in javascript 
Javascript :: array vowels 
Javascript :: js window.alert 
Javascript :: javascript is not null 
Javascript :: angular refresh token 
Javascript :: regex match exact string 
Javascript :: set js 
Javascript :: how to find length of a assocative array vuejs 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =