Im trying to verify last messages of my users, and verify if he is using selfbot in my application.
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?
I tried using this, but i do not know how to proceed now
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);
You can loops with recursion like this:
/*<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>
One way is to check one by one (perhaps not the most efficient):
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");