Estoy tratando de verificar los últimos mensajes de mis usuarios y verificar si está usando selfbot en mi aplicación.
var array = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"]; // ["a", "b", "c", "f", "b"] is the sequency, selfbot detected?Intenté usar esto, pero no sé cómo proceder ahora
var array = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"]; let counts = {}; array.forEach(x => { counts[x] = (counts[x] || 0) + 1; }); console.log(counts);Puede bucles con recursividad como este:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ const array = [ 'a', 'b', 'c', 'f', 'b', 'a', 'b', 'c', 'f', 'b', 'a', 'b', 'c', 'f', 'b', 'a', 'b', 'c', 'f', 'b', 'a', 'b', 'c', 'f', 'b', ]; let num = 0; (function findSeq(seqStart = 0, seqEnd = 0, i = 0, j = -1) { while (++i < array.length && array[i] !== array[seqStart]); seqEnd = i; while (++j < seqEnd && array[i++] === array[j]); if (seqEnd === j) console.log( `Sequence ${++num} found between`, seqStart, seqEnd, array.slice(seqStart, seqEnd) ); if (i < array.length) findSeq(seqEnd, 0, i - 1, j - 1); })(); console.log(`${num} sequences found`); <!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>Una forma es verificar uno por uno (quizás no sea el más eficiente):
var sequence = ["a", "b", "c", "f", "b"]; // Sequence that shows whether user is bot // Test cases var array1 = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"]; var array2 = ["b", "a", "b", "b", "f", "c", "a", "b", "c", "d", "e", "f", "a", "f", "c", "c", "c", "c", "f", "a", "a", "b", "b", "e", "e"]; function selfBotDetector(arr, seq) { // Function to check if sequence matches given range var check = start => { for(var t = start; t < seq.length + start; t++) { if(arr[t] !== seq[t - start]) { return false; } } return true; }; for (var i = 0; i < arr.length; i++) { var result = check(i); // If at any time the sequence matches return true if(result) return true; } // if true was never returned then the sequence did not match return false; } console.log("Array 1:", selfBotDetector(array1, sequence) ? "Selfbot detected" : "OK"); console.log("Array 2:", selfBotDetector(array2, sequence) ? "Selfbot detected" : "OK");