JavaScript Finding last index of repeating character string and relative frequency of character in Javascript finding last index of string in java,finding last index

I am working on a javascript problem for fun that is supposed to take a string and return nested arrays that contain one array for every character that repeats. The groups should have the following structure: [[value, first_index, last_index, times_repeated], ..., [value, first_index, last_index, times_repeated]]. With the current code I have the value and first index is always correct, but I am struggling to understand how I would find the last index of a character that is contained in a repeat, and also how to find the frequency of just that repeat.

  • value: Character being assessed.
  • first_index: Index of characters first appearance.
  • last_index: Index of characters last appearance.
  • times_repeated: Number of consecutive times character repeats.

Here is my code:

function findRepeating(str) {
const splitter = str.split("");
let arr = [];

const getOccurance = (array, value) => {
    let count = 0;
array.forEach((v) => (v === value && count++));
return count;
}

for(i = 0; i < splitter.length; i++){
    if(i === 0){
        let firstArr = [];
        firstArr.push(splitter[i], i, splitter.lastIndexOf(splitter[i]), getOccurance(splitter, splitter[i]));
        arr.push(firstArr);
    } else {
        if(splitter[i] !== splitter[i - 1]){
            let nArr = [];

            nArr.push(splitter[i], i, splitter.lastIndexOf(splitter[i]), getOccurance(splitter, splitter[i]));
            arr.push(nArr);
        } else {
            continue;
        }
    }
}
return arr;
}

Here are some expected outputs based on string inputs:

Test.assertSimilar(findRepeating(''), [])
Test.assertSimilar(findRepeating('a'), [['a', 0, 0, 1]])
Test.assertSimilar(findRepeating('1337'), [['1', 0, 0, 1], ['3', 1, 2, 2], ['7', 3, 3, 1]])
Test.assertSimilar(findRepeating('aabbb'), [['a', 0, 1, 2], ['b', 2, 4, 3]])
Test.assertSimilar(findRepeating('addressee'), [['a', 0, 0, 1], ['d', 1, 2, 2], ['r', 3, 3, 1], ['e', 4, 4, 1], ['s', 5, 6, 2], ['e', 7, 8, 2]])
Test.assertSimilar(findRepeating('aabbbaabbb'), [['a', 0, 1, 2], ['b', 2, 4, 3], ['a', 5, 6, 2], ['b', 7, 9, 3]])
Test.assertSimilar(findRepeating('1111222233334444'), [['1', 0, 3, 4], ['2', 4, 7, 4], ['3', 8, 11, 4], ['4', 12, 15, 4]])
Test.assertSimilar(findRepeating('1000000000000066600000000000001'), [['1', 0, 0, 1], ['0', 1, 13, 13], ['6', 14, 16, 3], ['0', 17, 29, 13], ['1', 30, 30, 1]])
Answer:1

Here is an alternative solution which could solve it. The .replace() method has a special replacement callback which you can use. You can pass it an argument of /(.)\1*/g which is a regular expression that matches groups of characters. The replacement method then gives the match (m, ie the group of characters), the single character it matched in the group c, and the index it was matched at i. Using all this, you can get all the information you need for your output:

const findRepeating = str => {
  const res = [];
  str.replace(/(.)\1*/g, (m, c, i) => res.push([c, i, i+m.length-1, m.length]));
  return res;
}

Test.assertSimilar(findRepeating(''), [])
Test.assertSimilar(findRepeating('a'), [['a', 0, 0, 1]])
Test.assertSimilar(findRepeating('1337'), [['1', 0, 0, 1], ['3', 1, 2, 2], ['7', 3, 3, 1]])
Test.assertSimilar(findRepeating('aabbb'), [['a', 0, 1, 2], ['b', 2, 4, 3]])
Test.assertSimilar(findRepeating('addressee'), [['a', 0, 0, 1], ['d', 1, 2, 2], ['r', 3, 3, 1], ['e', 4, 4, 1], ['s', 5, 6, 2], ['e', 7, 8, 2]])
Test.assertSimilar(findRepeating('aabbbaabbb'), [['a', 0, 1, 2], ['b', 2, 4, 3], ['a', 5, 6, 2], ['b', 7, 9, 3]])
Test.assertSimilar(findRepeating('1111222233334444'), [['1', 0, 3, 4], ['2', 4, 7, 4], ['3', 8, 11, 4], ['4', 12, 15, 4]])
Test.assertSimilar(findRepeating('1000000000000066600000000000001'), [['1', 0, 0, 1], ['0', 1, 13, 13], ['6', 14, 16, 3], ['0', 17, 29, 13], ['1', 30, 30, 1]]);
<script>
  const Test = {
    assertSimilar: (res, arr) => {
      console.log(...(JSON.stringify(res) !== JSON.stringify(arr) ? ["Unexpected result:", res, "expected:", res]: ["Test passed"]));
    }
  }
</script>
Answer:2

I've got a small problem in my javascript which i am stuck on for a while now. What i did is : Create an empty table. Generate the tr/td tags and values inside the table(from JSON-object). for (...

I've got a small problem in my javascript which i am stuck on for a while now. What i did is : Create an empty table. Generate the tr/td tags and values inside the table(from JSON-object). for (...

I have am trying to create jQuery code which adds a active-menu class to a link whenever I click on it. However I need to check first if it already has the class before adding. I only need the link ...

I have am trying to create jQuery code which adds a active-menu class to a link whenever I click on it. However I need to check first if it already has the class before adding. I only need the link ...

On my website, I have an slider with images. In the backend, the current slider images are displayed as well as a button that adds an input field, so I can upload new images for the slider. The code ...

On my website, I have an slider with images. In the backend, the current slider images are displayed as well as a button that adds an input field, so I can upload new images for the slider. The code ...

  1. get file from form php
  2. save file from form php
  3. file itr from form 16
  4. upload file from form
  5. php file from form
  6. upload file from form php
  7. upload file from form ajax
  8. send file from form
  9. download file from form
  10. post file from form
  11. submit file from form
  12. file from html form
  13. curl file from form
  14. file form 941 online
  15. file form d
  16. file form 941
  17. file form 990-n
  18. file form 1040
  19. file form 7004 online
  20. file form 4868 online

I have the form disabled when click edit form is enable <% while(resultset.next()){ %> <form method='POST' action='EditCompany?id=<%= resultset.getString(1)%>'> <tbody> &...

I have the form disabled when click edit form is enable <% while(resultset.next()){ %> <form method='POST' action='EditCompany?id=<%= resultset.getString(1)%>'> <tbody> &...

  1. enable button when form is valid angular
  2. enable button when form is valid
  3. enable submit button when form is valid
  4. enable button if form is valid angular 6