const testCases = [
{test:[[1,2,3],[5,3,2],[7,3,2]], result: 5},
{test:[[1,2,2,3],[5,3,2,2],[7,3,2,2]], result: 7},
{test:[[1],[1],[1]], result: 1},
{test:[[1],[1],[2]], result: 0},
];
const createRandomTests = (count) => {
const ARR_LENGTH = 5000;
const MAX_VALUE = 1000;
const common = (...args) => {
const storage = {};
let result = 0;
args.forEach((arr, index)=>{
for (const item of arr) {
if (index > 0 && !(storage[item] && storage[item][index - 1])) continue;
if (!storage[item]) {
storage[item] = [];
}
storage[item][index] = storage[item][index] ? storage[item][index] + 1 : 1;
}
});
for (const item of Object.keys(storage)) {
if (storage[item].length !== args.length) continue;
result += Math.min(...storage[item]) * item;
}
return result;
}
for (let i = 0; i < count; i+=1) {
const testCase = [[],[],[]];
for (let j = 0; j < ARR_LENGTH; j += 1) {
testCase.forEach(item => item.push(Math.floor(Math.random() * MAX_VALUE)));
}
const result = common(testCase);
testCases.push({test:testCase, result});
}
};
createRandomTests(10);
runTests = (cb) => {
testCases.forEach((item, index) => {
const result = cb(...item.test);
console.assert(result===item.result, {result, errorMsg:`Test${index +1}: expected ${item.result}`});
});
}
function common(...args){
const storage = {};
let result = 0;
args.forEach((arr, index)=>{
for (const item of arr) {
if (index > 0 && !(storage[item] && storage[item][index - 1])) continue;
if (!storage[item]) {
storage[item] = [];
}
storage[item][index] = storage[item][index] ? storage[item][index] + 1 : 1;
}
});
for (const item of Object.keys(storage)) {
if (storage[item].length !== args.length) continue;
result += Math.min(...storage[item]) * item;
}
return result;
}
runTests(common);
function common(a,b,c){
const result = {};
let final = 0;
for (const item of a) {
result[item] ? result[item][0]++ : result[item] = [1];
}
for (const item of b) {
if (!result[item]) continue;
result[item][1] ? result[item][1]++ : result[item][1] = 1;
}
for (const item of c) {
if (!result[item] || !result[item][1]) continue;
result[item][2] ? result[item][2]++ : result[item][2] = 1;
}
for (const item of Object.keys(result)) {
if (result[item].length !== 3) continue;
final += Math.min(...result[item]) * item;
}
return final;
}
runTests(common);
function common (a, b, c) {
let result = 0;
const createObj = (obj, key) => {
obj[key] = obj[key] ? obj[key] + 1 : 1;
return obj;
}
const [objA, objB, objC] = [a, b, c].map(n => n.reduce(createObj, {}));
for (const [key, value] of Object.entries(objA)) {
if (objB[key] && objC[key]) {
const min = Math.min(value, objB[key], objC[key]);
result += key * min;
}
}
return result;
}
runTests(common);
function common(a,b,c) {
let result = 0;
const arr = Array.from(arguments);
const refArr = arr.shift();
refArr.forEach(it => {
let duplicatesCounter = 1;
for (let i = 0; i < arr.length; i++) {
arr[i].includes(it) && duplicatesCounter++;
duplicatesCounter === arguments.length && (result += it);
}
});
return result;
};
runTests(common);
function common(a,b,c){
let sum = 0;
a.forEach( (item) => {
if (b.indexOf(item) >= 0 && c.indexOf(item) >= 0) {
sum += item;
b.splice(b.indexOf(item), 1);
c.splice(c.indexOf(item), 1);
}
});
return sum;
}
runTests(common);
function common(a,b,c){
return a.reduce((acc, item) => {
let indexB = b.indexOf(item);
let indexC = c.indexOf(item);
if ((indexB !== -1) && (indexC !== -1)) {
b.splice(indexB,1);
c.splice(indexC,1);
acc += item
}
return acc
}, 0)
}
runTests(common);
function common (a, b, c) {
let result = 0;
const createObj = (arr) => {
const obj = {};
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
obj[item] = obj[item] ? obj[item] + 1 : 1;
}
return obj;
}
const objA = createObj(a);
const objB = createObj(b);
const objC = createObj(c);
for (const [key, value] of Object.entries(objA)) {
if (objB[key] && objC[key]) {
const min = Math.min(value, objB[key], objC[key]);
result += key * min;
}
}
return result;
}
runTests(common);
function common(a, b, c) {
var result = 0;
let string = Array.from(arguments).toString().replace(/\s|,/g, '');
while (string.length) {
var indices = [];
var substr = string[0];
for (var i = 0; i < string.length; i++) string[i] === substr && indices.push(i);
if (indices.length && (indices.length % arguments.length === 0)) {
result += +substr * (indices.length / arguments.length)
}
string = string.replace(new RegExp(substr, 'g'), '');
}
return result
}
runTests(common);
function common(a, b, c) {
var result = 0;
const refObj =
Array.from(arguments)
.flat()
.reduce((acc, it) => {
acc[it] = (acc[it] || 0) + 1;
return acc
}, {});
for (const key in refObj) {
if (refObj[key] && refObj[key] % arguments.length === 0) {
result += +key * (refObj[key] / arguments.length)
}
}
return result
}
runTests(common);