@Ilgiz
Оно? Или равномерность не устраивает, так как идут подряд значения?
три
два
три
два
три
два
три
два
три
два
три
два
три
два
три
два
три
два
три
два
три
один
три
один
три
один
три
ge
три
function distributeEvenly(arr) {
// Function to get the next index, skipping to enforce even distribution
function getNextIndex(index, skip, length) {
return (index + skip) % length;
}
// Create a map to count the frequency of each element
const map = arr.reduce((acc, e) => {
acc[e] = (acc[e] || 0) + 1;
return acc;
}, {});
// Sort items by count descending
const sortedItems = Object.keys(map).sort((a, b) => map[b] - map[a]);
// Calculate the minimum skip distance based on the most frequent item
const maxCount = map[sortedItems[0]];
const minSkip = Math.floor(arr.length / maxCount);
// Initialize the result array
const result = [];
// Distribute items starting from the most frequent
for (let i = 0; i < sortedItems.length; i++) {
let count = map[sortedItems[i]];
let index = i;
while (count > 0) {
// Find the next available index
while (result[index] !== undefined) {
index = getNextIndex(index, minSkip, arr.length);
}
result[index] = sortedItems[i];
index = getNextIndex(index, minSkip, arr.length);
count--;
}
}
return result;
}
const inputArray = [
'один', 'один', 'один', 'один', 'один',
'два', 'два', 'два', 'два', 'два', 'два', 'два', 'два', 'два', 'два',
'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три', 'три',
'ge', 'gee', 'baba'
];
const evenlyDistributedArray = distributeEvenly(inputArray);
console.log(evenlyDistributedArray.join('\n'));