Counting unique duplicates gone wrong

Viewed 77

I came across this challenge in which you have to count how many duplicates there are in the string provided.

I came up with this code that is supposed to sort the string if there are duplicates splice them and count them as +1 to the total. If the letter is not a duplicate -> just splice them so I can move forward.

What is going on with the code? I do not want an answer for the challenge but to understand where I went wrong. Why is the code registering the duplicates only if there is 4+ of the same letter?

Here is the code, note that I am only using the var e to see what part of the array is getting spliced.

The expected output:

[[p, p], [a, a, a, a], [c, c, c, c, c, c, c, c], [d, d, d, d], [g, g, g], [e, e]]

If there is 2+ occurrences of the same character slice that chunk of duplicates out and put them in a new array.

function duplicateCount(text) {
  const str = Array.from(text.toLowerCase()).sort();
  var h = 0;
  var e = [];
  for (let i = 0; i < str.length; i++) {
    if (str.lastIndexOf(str[i]) > 2) {
      h++;
      e.push(str.splice(0, str.lastIndexOf(str[i]) + 1));
      i = 0;
    } else {
      str.splice(0, 1);
      i = 0
    }
  }
  return e
}
console.log(duplicateCount('ppaaaaccccccccgggiyddddhee'));

2 Answers

I think this can be done better with Regex and map() to separate out each string our regex matched. We are then left with an array of string arrays, as you asked.

(.) - 1st capturing group - matches any character

\1 - matches same text as 1st capturing group

+ - matches between 1 and unlimited times

const testString = "ppaaaaccccccccgggiyddddheep";

function getAllDuplicates(str) {
  return str.split("") // split by character
    .sort() // sort the array
    .join("") // join back into sorted string
    .match(/(.)\1+/g) // match repeated characters
    .map(string => string.split("")); // use map to split each string in arr to get arr of strings
}

function getConsecutiveRepeating(str) {
  return str.match(/(.)\1+/g) // match repeated characters
    .map(string => string.split("")); // use map to split each string in arr to get arr of strings
}

console.log(getAllDuplicates(testString));
console.log(getConsecutiveRepeating(testString));

Your code sets the index back to zero when it removes the match. Problem with that is the next iteration in the loop makes it start at index 1, not 0. So you need to set it to -1. You actually would be better with a while loop.

Next issue is you are saying that the next index has to be > 2. Issue with that is what is the index with pp? It is not two. You want to make sure the index is not the same element.

function duplicateCount(text) {
  const str = Array.from(text.toLowerCase()).sort();
  var h = 0;
  var e = [];
  for (let i = 0; i < str.length; i++) {
    if (str.lastIndexOf(str[i]) != i) {
      h++;
      e.push(str.splice(0, str.lastIndexOf(str[i]) + 1));
      i = -1;
    } else {
      str.splice(0, 1);
      i = -1
    }
  }
  return e
}
console.log(duplicateCount('ppaaaaccccccccgggiyddddhee'));

with a while loop

function duplicateCount(text) {
  const str = Array.from(text.toLowerCase()).sort();
  var h = 0;
  var e = [];
  while (str.length) {
    var lastIndex = str.lastIndexOf(str[0])
    var removed = str.splice(0, lastIndex + 1);
    if (lastIndex) {
      h++;
      e.push(removed);
    }
  }
  return e
}
console.log(duplicateCount('ppaaaaccccccccgggiyddddhee'));

Related