JSFiddle - React, Tailwind, and code Playground
by Amir Danish
JavaScript
function count(arr) {
arr.sort();
var current = null;
var cnt = 0;
var tdup=0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] != current) {
if (cnt > 0 || cnt <= 0) {
/* console.log(cnt-1) */;
document.write(current + '> ' + (cnt-1));
}
current = arr[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
document.write(current + '-> ' + cnt);
}
}
function countDuplicates(arr) {
const uniqueItems = new Set();
const duplicates = new Set();
for (const value of arr) {
if (uniqueItems.has(value)) {
duplicates.add(value);
uniqueItems.delete(value);
} else {
uniqueItems.add(value);
}
}
if(duplicates.size==0){
return duplicates.size;
}else { return duplicates.size+1;}
}
count([0, -2, -2, 5, 5, 5]);
console.log(countDuplicates( [0,-2,-21,25,15,35] ))
/*
function MathChallenge(num) {
// when i check it on jsfiddle.net the result is correct but here it not showing the expected out put
// plz check my answer on jsfiddle.net too
// code goes here
function addition(a,b){ return Number(a)-Number(b)}
var number = num.toString()
var str = number.split("");
let numberArray = [];
for(var i=0; i<str.length-1; i++){
let curentAndnextNumber = str[i]+""+str[i+1];
numberArray.push(curentAndnextNumber);
}
let sortedArray = numberArray.sort(addition);
return sortedArray.shift();
// ***** if you wanna check the final result should return in integer format. ***
// const fResult=sortedArray.shift();
// if(isNaN(fResult)==true) {
// return fResult;
// }else{
// return parseInt(fResult)
// }
}
// keep this function call here
console.log(MathChallenge(363223311)); */