Skip to content

Instantly share code, notes, and snippets.

@CardealRusso
Last active February 24, 2023 12:01
Show Gist options
  • Save CardealRusso/fc6f63321561d391a248e3e3390b8ec4 to your computer and use it in GitHub Desktop.
Save CardealRusso/fc6f63321561d391a248e3e3390b8ec4 to your computer and use it in GitHub Desktop.
Find adjacent duplicates in js (for basic flood protection)
const test = "imbecile kkk imbecile kkk imebicle kkk";
const results = {};
for (let i = 0; i < test.length; i++) {
for (let j = i + 1; j <= test.length; j++) {
const sequence = test.slice(i, j);
if (sequence === test.slice(j, j + sequence.length)) {
if (sequence in results) {
results[sequence]++;
} else {
results[sequence] = 1;
}
}
}
}
console.log(results); // {"imbecile kkk ": 1, mbecile kkk i: 1, becile kkk im: 1, k: 6}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment