let mostVisitedPattern = function(username, timestamp, website) { let map = new Map(); for (let i = 0; i < username.length; i++) { if (map.has(username[i])) { map.get(username[i]).push([website[i], timestamp[i]]); } else { map.set(username[i], [[website[i], timestamp[i]]]); } } // get all possibilites let visitedPattern = {}; let possibilityMap = new Map(); for(let [user, visits] of map.entries()) { visitedPattern[user] = {}; const value = visits.sort((a, b) => a[1] - b[1]).map(([a]) => a); if ( value.length < 3) continue; for (let i = 0; i < value.length - 2; i++) { for (let j = i + 1; j < value.length - 1; j++) { for (let k = j + 1; k < value.length; k++) { let key = `${value[i]},${value[j]},${value[k]}`; if(possibilityMap.has(key)) { if(!visitedPattern[user][key]) { possibilityMap.set(key, possibilityMap.get(key) + 1); visitedPattern[user][key] = 1; } } else { possibilityMap.set(key, 1); visitedPattern[user][key] = 1; } } } } } const result = [...possibilityMap]; return result.sort((a, b) => { if(a[1] < b[1]) { return 1; } else if (a[1] > b[1]) { return -1; } return a[0].localeCompare(b[0]); }).map(arr => arr[0]).shift().split(','); //? // result.pop(); //? // .map(arr => arr[0]).pop().split('|'); //? };