如何获取字符串中重复的字母
How to get the repeated letter in a string?
var a = "aaaaaaaaaabbffddcccuuekkjjmlotcfshhh";
在这里,我想得到输入的最大值和最小值。例如,我输入了10次字母"a"。这是最大值。所以我想低估这个价值。如何使用jquery或javascript获取字符串中输入的最大值。
具有Array#forEach()
和对象count
:的提案
var a = "aaaaaaaaaabbffddcccuuekkjjmlotcfshhh",
count = {}, min, max;
a.split('').forEach(function (a) {
count[a] = (count[a] || 0) + 1;
});
Object.keys(count).forEach(function (a, i) {
if (!i) {
min = [a];
max = [a];
return;
}
if (count[a] < count[min[0]]) {
min = [a];
return;
}
if (count[a] > count[max[0]]) {
max = [a];
return;
}
if (count[min[0]] === count[a]) {
min.push(a);
}
if (count[max[0]] === count[a]) {
max.push(a);
}
});
document.write('min: ' + min.join(', ') + ' with occurrence of ' + count[min[0]] + '<br>');
document.write('max: ' + max.join(', ') + ' with occurrence of ' + count[max[0]] + '<br>');
document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');
如何使用jquery或javascript。
试试这个
var a = "aaaaaaaaaabbffddcccuuekkjjmlotcfshhh";
var allchars = a.split("").sort();
var output = {};
allchars.forEach(function(val){ output[val] = output[val] || 0; output[val]++; });
现在找到值最高的密钥
var highestProp = "";
var highestValue = 0;
var lowestProp = "";
var lowestValue = Number.MAX_SAFE_INTEGER;
Object.keys(output).forEach(function(key){
if (output[key] > highestValue )
{
highestValue = output[key];
highestProp = key;
}
if (output[key] < lowestValue )
{
lowestValue = output[key];
lowestProp = key;
}
});
现在highestValue
和highestProp
具有最高值和最高值的属性名称。类似地,lowestValue
和lowestProp
具有最低值和最低值属性名称。
演示
var a = "aaaaaaaaaabbffddcccuuekkjjmlotcfshhh";
var allchars = a.split("").sort();
var output = {};
allchars.forEach(function(val){ output[val] = output[val] || 0; output[val]++; });
var highestProp = "";
var highestValue = 0;
var lowestProp = "";
var lowestValue = Number.MAX_SAFE_INTEGER;
Object.keys(output).forEach(function(key){
if (output[key] > highestValue )
{
highestValue = output[key];
highestProp = key;
}
if (output[key] < lowestValue )
{
lowestValue = output[key];
lowestProp = key;
}
});
alert(highestProp + " has the highest frequency of " + highestValue);
alert(lowestProp + " has the lowest frequency of " + lowestValue);
相关文章:
- NodeJS在一个较大的字符串中获取一个字符串的索引
- Javascript从字符串中获取顶级域
- 如何在javascript中从字符串中获取月份
- Jquery - 从查询字符串中获取“#”值之后,而不是在“?”符号值之后获取
- 从数字字符串中获取整数
- 如何从字符串中获取两个变量
- 从javascript中的字符串路径获取文件名
- 从字符串中获取价格
- 如何从字符串中获取数据集
- 从查询字符串中获取变量,将其与Javascript中的数组匹配
- 从包含密钥路径的字符串中获取json值
- 如何使用html5中的javascript在页面加载时从查询字符串中获取值
- 如何从以某个单词(regexp)结尾的字符串中获取id
- 从子字符串之后的字符串中获取字符串
- RegEx 用于在 Json 字符串中获取值
- 从路径字符串 - Javascript 获取所有父路径
- 如何从javascript或jquery中的时间字符串中获取分钟数
- 从字符串中获取星期几的Javascript方法
- 在Javascript中从逗号分隔的字符串中获取所有值
- 从 javascript 中的字符串中获取差值(小时)