返回最常见字母的javascript代码有什么问题?

返回最常见字母的javascript代码有什么问题?,javascript,Javascript,我正在尝试编写一个解决方案,返回字符串中最常见的字母,以及该字母出现的次数。 我提出的解决方案是在字符串中循环,每次在一个名为letter的变量中保存一个字母,并将所有其他字母与此进行比较。一旦字符串结束,循环将保留第二个字母并重复 我遇到的问题是它只返回字符串中的第一个字母。它获得了正确的匹配数,但忽略了其他字母具有更多匹配项的事实 我做错了什么 var匹配=0; var-matchCount=0; var字母=0; var计数=0; indx1=0; indx2=0; 函数问题十(a){

我正在尝试编写一个解决方案,返回字符串中最常见的字母,以及该字母出现的次数。 我提出的解决方案是在字符串中循环,每次在一个名为letter的变量中保存一个字母,并将所有其他字母与此进行比较。一旦字符串结束,循环将保留第二个字母并重复

我遇到的问题是它只返回字符串中的第一个字母。它获得了正确的匹配数,但忽略了其他字母具有更多匹配项的事实

我做错了什么

var匹配=0;
var-matchCount=0;
var字母=0;
var计数=0;
indx1=0;
indx2=0;
函数问题十(a){
while(indx1matchCount){//如果是第一次,或者如果此字母比前一个字母有更多匹配项
match=letter;//将字母保存在match变量中
matchCount=count;//保存count变量中的匹配数
}
indx1+=1;//循环使用比较的第一个变量
}
return[match,matchCount];//返回结果
}

控制台日志(问题(“收敛”)按原样,代码只在字符串中循环一次。为此,应该在内部while循环之前将indx2重置为0。 完成此操作后,还必须在每个内部循环中重置“count”变量

一种优化方法是让indx2从indx1+1开始,并在内部循环的开始处使count=1

var匹配=0;
var-matchCount=0;
var字母=0;
var计数=0;
indx1=0;
indx2=0;
函数问题十(a){
while(indx1indx2=0;//也许可以检查此解决方案,它的时间复杂度与您尝试实现的时间复杂度相比是线性的,即四分之一:

function problemTen(a) {
  var array = a.split('');
  var max = 0;
  var letter;

  var counter = array.reduce(function(memo, item) {
    memo[item] = memo[item] ? memo[item] + 1 : 1;
    if (memo[item] > max) {
      max = memo[item];
      letter = item;
    }
    return memo;
  }, {});

  return [letter, max];
}

console.log(problemTen("astrings"));
如果您想坚持您的解决方案,问题是,当您再次运行外部while循环时,您没有重置indx2和count变量,因此,indx2=a.length已经存在,而内部while循环不再运行以计数字母。添加这些重置后,您的代码应该如下所示:

function problemTen(a) {
  var match = 0;
  var matchCount = 0
  var letter = 0;
  var count = 0;
  indx1 = 0;
  indx2 = 0;

  while (indx1 < a.length) {
    letter = a[indx1]; //hold the first letter in the string in the letter variable

    while (indx2 < a.length) { //now rotate through every other letter to compare
      if (a[indx2] === letter) { //compare 
        count += 1; // if there is a match, add one to the count variable to hold the number of matches
      }
      indx2 += 1; //cycle through the rest of the string  
    }
    if (matchCount === 0 || count > matchCount) { // if it’s the first time around,     or if this letter had more matches than the previous letter
      match = letter; // hold the letter in the match variable
      matchCount = count; //hold the number of matches in the count     variable
    }
    indx1 += 1; //cycle through the first variable that you compare

    // HERE WE RESET indx2 and match to 0
    indx2 = 0;
    match = 0;

  }
  return [match, matchCount]; //return results
}

console.log(problemTen("astrings"));
函数问题十(a){
var匹配=0;
var matchCount=0
var字母=0;
var计数=0;
indx1=0;
indx2=0;
while(indx1matchCount){//如果是第一次,或者如果此字母比前一个字母有更多匹配项
match=letter;//将字母保存在match变量中
matchCount=count;//保存count变量中的匹配数
}
indx1+=1;//循环使用比较的第一个变量
//这里我们重置indx2并将其匹配为0
indx2=0;
匹配=0;
}
return[match,matchCount];//返回结果
}
控制台日志(问题(“收敛”);
事实上,要实现O(n)时间复杂度,您不必使用reduce,也可以使用一个循环来实现,而且肯定会快得多,我更喜欢函数式编程,因为它具有更好的可读性:

function problemTen(a) {
  var max = 0;
  var letter;
  var counter = {};

  for (var i = 0, l = a.length; i<l; i++) {
    counter[a[i]] = counter[a[i]] ? counter[a[i]] + 1 : 1;
    if (counter[a[i]] > max) {
      max = counter[a[i]];
      letter = a[i];
    }
  }

  return [letter, max];
}

console.log(problemTen("astrings"));
函数问题十(a){
var max=0;
var字母;
var计数器={};
对于(变量i=0,l=a.length;i max){
最大值=计数器[a[i]];
字母=a[i];
}
}
返回[字母,最大值];
}
控制台日志(问题(“收敛”);
希望这有帮助。

您的任务是找到(第一个)给定字符串中出现次数最多的字符。如果您不想像Ahmet Cetin的回答那样使用额外内存,您确实需要在此处使用两个循环。您还需要对当前字符及其出现次数进行一次循环,对actaul top Position及其出现次数进行一次循环,以及四个变量

这是一个
for
循环比
while
循环更适合的地方,但两者都可以工作

var teststring = "teststring";

function problemTen(s){
  var count = 0;      // counter for current character
  var last = 0;       // counter for actual highest character
  var character = ""; // current character
  var top = "";       // highest character

  for(var i = 0;i < s.length;i++){     // Loop over the full string
    character = s[i];                  // set current character
    for(var j = 0;j < s.length; j++){  // Loop over the full string
      if(s[j] === character){          // we found another occurence
        count++;                       // increment the counter
      }
    }
    if(last < count){   // the actual char. has more occ. than the last one
      last = count;     // set the top counter to the current counter
      top = character;  // set the top character to the current character
    }
    count = 0;        // reset the current counter
  }
  return [top,last];    // return the character and count of the to one
}

problemTen(teststring);
var teststring=“teststring”;
函数问题十(s){
var count=0;//当前字符的计数器
var last=0;//实际最高字符的计数器
var character=”“;//当前字符
var top=”“;//最高字符
对于(var i=0;i
可能的重复项您是否曾经将
indx2
设置回
0
?不相关的旁注:为什么所有变量都是全局变量?感谢您的解决方案!我最终切换到使用for循环并实现了