Last active
February 24, 2023 12:01
-
-
Save CardealRusso/fc6f63321561d391a248e3e3390b8ec4 to your computer and use it in GitHub Desktop.
Find adjacent duplicates in js (for basic flood protection)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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