Karol Pośpiech
Karol Pośpiech

Reputation: 23

counting the same letters in a string but not in all lenght

I'm starting my adventure with javascript and i got one of first tasks. I must create function that count letter that most occur in string and write this in console. For example: var string = "assssssadaaaAAAasadaaab";

and in console.log should be (7,a) <--- the longest string is 7 consecutive identical characters (yes, before count i use .toLowerCase();, because the task requires it)

So far I have it and I don't know what to do next. Someone want to help?

var string = "assssssadaaaAAAasadaaab";
var string = string.toLowerCase();
function writeInConsole(){
  console.log(string);
  var count = (string.match(/a/g) || []).length;
  console.log(count);
}

writeInConsole();

Upvotes: 1

Views: 175

Answers (3)

The fourth bird
The fourth bird

Reputation: 163352

One option could be matching all consecutive characters using (.)\1* and sort the result by character length.

Then return an array with the length of the string and the character.

Note that this will take the first longest occurrence in case of multiple characters with the same length.

function writeInConsole(s) {
  var m = s.match(/(.)\1*/g);
  if (m) {
    var res = m.reduce(function(a, b) {
      return b.length > a.length ? b : a;
    })
    return [res.length, res.charAt(0)];
  }
  return [];
}

["assssssadaaaAAAasadaaab", "a", ""].forEach(s => {
  s = s.toLowerCase();
  console.log(writeInConsole(s))
});

Another example when you have multiple consecutive characters with the same length

function writeInConsole(s) {
  let m = s.match(/(.)\1*/g);

  if (m) {
    let sorted = m.sort((a, b) => b.length - a.length)
    let maxLength = sorted[0].length;
    let result = [];

    for (let i = 0; i < sorted.length; i++) {
      if (sorted[i].length === maxLength) {
        result.push([maxLength, sorted[i].charAt(0)]);
        continue;
      }
      break;
    }
    return result;
  }
  return [];
}

[
  "assssssadaaaAAAasadaaab",
  "aaabccc",
  "abc",
  "yyzzz",
  "aa",
  ""
].forEach(s => {
  s = s.toLowerCase();
  console.log(writeInConsole(s))
});

Upvotes: 3

Thomas Stenhaug
Thomas Stenhaug

Reputation: 109

The answer given by using regular expressions is more succinct, but since you say you are just starting out with programming, I will offer a verbose one that might be easier to follow.

var string = "assssssadaaaAAAasadaaab";
var string = string.toLowerCase();

function computeLongestRun(s) {
    // we set up for the computation at the first character in the string
    var longestRunLetter = currentLetter = string[0]
    var longestRunLength = currentRunLength = 1

    // loop through the string considering one character at a time
    for (i = 1; i < s.length; i++) {
        if (s[i] == currentLetter) { // is this letter the same as the last one?
            currentRunLength++            // if yes, reflect that
        } else {                          // otherwise, check if the current run
                                          // is the longest
            if (currentRunLength > longestRunLength) {
                longestRunLetter = currentLetter
                longestRunLength = currentRunLength
            }
            // reset to start counting a new run
            currentRunLength = 1
            currentLetter = s[i]
        }
    }
    return [longestRunLetter, longestRunLength]
}

console.log(computeLongestRun(string))

Upvotes: 1

Carlos  Robles
Carlos Robles

Reputation: 68

I'm no sure if this works for you:

string source = "/once/upon/a/time/";
int count = 0;
foreach (char c in source) 
  if (c == '/') count++;

Upvotes: 1

Related Questions