function getMostCommonPair(str) {
// Object to store pairs and their counts
const pairCounts = {};
const size = str.length;
// Iterate through the string
for (let i = 0; i < size - 1; i++) {
// Get the current pair of characters
const pair = str[i] + str[i + 1];
if (pairCounts[pair]) {
pairCounts[pair]++;
} else {
pairCounts[pair] = 1;
}
}
// Find the pair with the highest count
let maxPair = '';
let maxCount = 0;
for (const pair in pairCounts) {
if (pairCounts[pair] > maxCount) {
maxCount = pairCounts[pair];
maxPair = pair;
}
}
console.log("Max count: " + maxCount);
return maxPair;
}
const str = "xzvxdesxzhdeaalzxzmdenlopxzxzxzaaqdewrzaaaapeerxzxz";
console.log("The most common pair is: " + getMostCommonPair(str));
/*
run:
Max count: 8
The most common pair is: xz
*/