preface

Nothing to do, poor toss. Recently, when my friend is looking for a job, he will timely discuss with me when he meets some interview questions or problems. And I’m his brains behind the scenes, giving him advice. I’m going to share with you a simple interview question.

The title

What are the most letters and numbers in a string?

Analysis of the

1, verify whether the data is a string 2, string to array 3, count the number of string occurrencesCopy the code

Ex. :

var str = 'aaaadddddssssgdhssssbbbbbuuuwmopqlsabcfwsqdghgukssuyutsudddddsasss';
Copy the code

Write a function that deals with that.

/** ** checks for the most frequent occurrences in the string ** /functionMaximum (s) {//if(typeof s ! = ='string') {
       throw ('s is string'); Var array = s.split();""); // Find the most letters // this is the most important step}Copy the code

In fact, the most important step is the third step, depending on what kind of train of thought you stand to find the corresponding letter and its number. Most people choose to use two loops for processing.

The code is as follows:

/** * checks the most frequent occurrences of the string */function maximum(s) {
  if(typeof s ! = ="string") {
    throw "s is string";
  }
  var array = s.split(""),
    l = array.length,
    obj = {},
    ismaxObj = {
      max: 0,
      val: ""
    },
    arrayVal = "";
  while (l--) {
    arrayVal = array[l];
    if(! obj[arrayVal]) { obj[arrayVal] = []; } obj[arrayVal].push(arrayVal);if(ismaxObj.max ! = 0) {if(obj[arrayVal].length > ismaxObj.max) { ismaxObj.max = obj[arrayVal].length; ismaxObj.val = array[l]; }}else{ ismaxObj.max = obj[arrayVal].length; ismaxObj.val = arrayVal; } } //console.log(array); console.log(ismaxObj); Console. log(obj); // Console. log(obj); } maximum("Aaaadddddsgdhssssbbbbbuuupqlsabcfwsqdghgukssuyutsudddddsasss ewfd, null, undefind.? Qzadsdvsf\/\\\\[;ll;,lw"
);
Copy the code

The result looks like this:

ask

If the interviewer asks, how do you determine the first and last positions of the most letters? In fact, when we push, we can save the location of this data, and we can solve it with a little change.

/** * checks the most frequent occurrences of the string */function maximum(s) {
  if(typeof s ! = ="string") {
    throw "s is string";
  }
  var array = s.split(""),
    ll = array.length,
    obj = {},
    ismaxObj = {
      max: 0,
      val: ""
    },
    arrayVal = "";
  for(var l=0; l<ll; l++) { arrayVal = array[l];if(! obj[arrayVal]) { obj[arrayVal] = []; } obj[arrayVal].push(arrayVal+The '-'+l);
    if(ismaxObj.max ! = 0) {if(obj[arrayVal].length > ismaxObj.max) { ismaxObj.max = obj[arrayVal].length; ismaxObj.val = array[l]; }}else{ ismaxObj.max = obj[arrayVal].length; ismaxObj.val = arrayVal; } } console.log(obj[ismaxObj.val][0],obj[ismaxObj.val][obj[ismaxObj.val].length-1]); Console. log(ismaxObj); // Console. log(ismaxObj); Console. log(obj); // Console. log(obj); }Copy the code

conclusion

In fact, for the interview, the most important thing is to test the logical thinking of the interviewer to deal with the problem. Some people can solve the problem, and some people can solve the problem with the best solution. The former is the man who does the work, the latter is the man who does the good work.