Skip to content

Commit

Permalink
Update 编写一个输出数组中出现次数最多的元素.js
Browse files Browse the repository at this point in the history
findeMore使用于各种字符串、数组判断出现次数最多的元素或者字符
  • Loading branch information
Rain120 authored Aug 7, 2018
1 parent ae107a1 commit f204843
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 编写一个输出数组中出现次数最多的元素.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ function findMost(arr) {
}
return maxEle
}

const findMost = (arr) => {
let res = arr.toString().split('').sort().join("").match(/(\w)\1+/g);
res.sort(function(a,b) {
return a.length - b.length;
})
console.log('res is : ' + res[res.length-1])
}

findMost([1, 2, 3, 1, 2, 5, 6, 6, 7, 4, 3, 2, 1, 4, 8, 1, 5])

0 comments on commit f204843

Please sign in to comment.