Home > Back-end >  Javascript get array element from the condition of summation of array element
Javascript get array element from the condition of summation of array element

Time:07-14

I have an Array, arr = [2,4,8,7,3,6] I want to make each element of it be summation when the result is 10 , then save the element it would be arranged to another array.

make the element that result is 10 close each other like 2 and 8, add to another element named arr2.

result i need : arr2[2,8,3,7,4,6]

my code :

const arr = [2,4,8,7,3,6]; let arr2 = [];


for (let i =0 ;i < arr.length();i  ){
let Number1 = arr[i];
let Number2 = arr[(i 1)];

if (Number1   Number2 === 10){
    let element1 = arr.indexOf(Number1);
    let element2 = arr.indexOf(Number2);
    arr2.push(element1,element2);
}

console.log(arr2[i]);}

someone can solve my problem please ?

CodePudding user response:

If you need to create arr2 so that the items sum up to 10 you can make use of a simple map here:

const arr = [2, 4, 8, 7, 3, 6];

const arr2 = arr.map((item) => 10 - item)

console.log(arr2);

CodePudding user response:

You should first loop through the array to create a dictionary of value to index, then loop the array again and lookup for the complement of the current value to the target. If it exist then yes you got the answer.

.filter(x => x > i) is to search for complement that has higher index than current one so that we will not get duplicated result pushed. For example input is [2, 8], you don't want to get [2, 8, 8, 2]

Here is my solution: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhgJwTAvDA2gJgDQwCy4AcuA7LgMy4BsAugNwBQANgKayIKaob0MMBmAVzDAoAS3AwIIJoPHgAFFEQBzNgC4YYQQFsARiwS4xYAA5zN2-YfQ0AlJd0GEtmAG8GMLzFCRYCFghBJihHaxcabltGbx9waBgmEBAAawBVUwBZOFNNN3QUlgBPMOcaUpsaAF9uNyrGT29WWDFuAAYY734QZAVfBJY4YAALGBB GBNzKDt3RtivMQmFAEIFQZHJsETk9Kycu1mPBZOd1Izs03QN4ci0aPmFqr5Ts73L66HbgDpzCGGFGI7A1XmIANRgzpeZ6PVpoDqPbq9fqwG5jCZTORHR6xFEwAAmS34hhYIhY3GUCDUsAAtDAbiDTksYApCfxiQEyVs3hcDnNXrj4rBQDpTKwdKSoABJMD4lgAD24SXO yubI5pOALBo334YhChgUipQAD4YIqzUD0G1eALvDimcsRWKWBKwNLZQrsXaTgEgiFfoJ-oCzHJ0GIaMZQ1B0M7xZKZXL5fYoQLnq8YaCIanM94AlBBAhtn7glBGDCOFw0NJZPIwAoAIxtXAcYF8foyFjfJIqBSV4FAA

const arr = [2, 4, 8, 7, 3, 6];
let arr2 = [];

function solution(target: number, input: number[]): number[] {
    const result: number[] = [];
    const lookUpMap: {[key: number]: number[]} = {};

    let i = 0;
    for (const each of input) {
        if (!(each in lookUpMap)) {
            lookUpMap[each] = [];
        }

        lookUpMap[each].push(i);

        i  ;
    }

    i = 0;
    for (const each of input) {
        const difference = target - each;

        if (difference in lookUpMap) {
            const complementIndex = lookUpMap[difference].filter(x => x > i)[0];
            
            if (complementIndex) {
                result.push(input[i], input[complimentingIndex]);
            }
        }

        i  ;
    }

    return result;
}

arr2 = solution(10, arr);

console.log(arr2);
  • Related