JSFiddle - React, Tailwind, and code Playground
by DineshAngappa
JavaScript
var arr = [6,8,4,4,4,5,5,1,3,2,2,7,7,5]; //[1,3,5,5,5,4,4,4,6,7,7,2,2,8]
var temp = 0;
/*for(var i=0; i< arr.length; i++){
for(var j=1; j < (arr.length - i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}*/
console.log(arr.sort());
var current = null;
var cnt = 0;
var temp = new Array();
for (var k = 0; k <= arr.length; k++){
if(arr[k] !== current){
if(cnt >0){
console.log(current,cnt+ ' times ');
temp.push([cnt, current]);
//temp.push(obj);
}
current = arr[k];
cnt = 1
}else{
cnt++;
}
}
console.log(temp.sort());
temp.sort(function(a,b){
console.log('a', a);
if(b[0] ==1 && a[0] >1){
console.log('b', b)
return a[1] - b[1];
}
}).sort(function(a,b){
if(b[0]>1){
if(b[0] === a[0]){
return b[1] - a[1];
}else{
return b[0] - a[0];
}
}
//return a[1] - b[1];
});
console.log(temp);