How to count number of duplicate e-s in the array?

let arr = [1,3,3,4,5,5,5];
function count(arr){
 let cnt = 1;
 let map = new Map();
 for(let i = 0; i< arr.length; i++){
 for(let j = 0; j< arr.length; j++){
 if(arr[i] == arr[j]){
map.set(arr[i],cnt++);
}
}
 } return map;
}
console.log(count(arr)); // map should be {1 => 1, 3 => 2, 4 => 1, 5 => 3} ,
// but it gives {1 => 1, 3 => 5, 4 => 6, 5 => 15}
// How to implement this? If you can explain what to do with this code(and indicate the error in 
// code) so he began to work correctly.
April 4th 20 at 13:30
4 answers
April 4th 20 at 13:32
Solution
function count(arr){
 const map = new Map();
 arr.forEach(i => map.set(i, (map.get(i) || 0) + 1))
 return map;
}
Please explain what makes this part (map.get(i) || 0) + 1) - brennan.Wilkins commented on April 4th 20 at 13:35
@brennan.Wilkins, Receives the number of repetitions from the vault. If there's nothing to take 0 (zero). To add to number 1.

Short version:
if (map.has(number)) {
 numberCount = map.get(number)
} else {
 numberCount = 0
}

numberCount = numberCount + 1
- Dante_Jast commented on April 4th 20 at 13:38
@brennan.Wilkins, takes the value of i element or if it is not 0 and adds 1 to it.
Probably will work like this code:
++map.get(i) but there is magic involved in the transformation booleana in integer. - Arne_Mann commented on April 4th 20 at 13:41
@Dante_Jast, Thank you very much for the explanation! - brennan.Wilkins commented on April 4th 20 at 13:44
April 4th 20 at 13:34
const count = array => array.reduce((accumulator, value) => {
 const total = accumulator.has(value) ? accumulator.get(value) : 0;

 accumulator.set(value, total + 1);

 return accumulator;
}, new Map());
April 4th 20 at 13:36
I like about do so:
let arr = [1,3,3,4,5,5,5];
obj = new Object();
arr.forEach((el)=>(obj.hasOwnProperty(el)) ? obj[el]++ : obj[el] = 1);
console.log(obj); // Object { 1: 1, 3: 2, 4: 1, 5: 3 }

well, that is, use the object property - the uniqueness of the keys
April 4th 20 at 13:38
let arr = [1,3,3,4,5,5,5];
function count(arr){
 let cnt = 1; // one counter for all values?
 let map = new Map();
 for(let i = 0; i< arr.length; i++){
 for(let j = 0; j< arr.length; j++){ // bad, that for each element in the whole array sorted
 // it turns out, all pairs are considered the same for each pass.
 if(arr[i] == arr[j]){ // here every time there is equality itself
 map.set(arr[i],cnt++); // and General counter more and more
}
}
 } return map;
}
console.log(count(arr));


To solve the problem of counting do not need nested loops.
Go through each element just once.
And to make the "dictionary": 1 if there was no such or increase by 1 if already.

fixed your code
let arr = [1,3,3,4,5,5,5];
function count(arr){
 const map = new Map();
 const int len = arr.length;
 for (let i = 0; i < len; i++) {
 const value = arr[i];
 if (!map.get(value)) map.set(value, 0); // create a counter
 map.set(value, map.get(value) + 1); // increment by 1
}
 return map;
}
console.log(count(arr));
/*
Map(4)
 size: 4
<entries>
 0: 1 → 1
 1: 3 → 2
 2: 4 → 1
 3: 5 → 3
*/

Find more questions by tags JavaScriptAlgorithmsArrays