2

I am trying to create a new array from existing array. Condition is that if element is duplicate then sum those elements. Please see below example for details as may be I am not able to explain clearly:

Input:

var arrayA = ["2", "1", "4", "2", "5", "1", "3", "2"];

Output:

var arrayB = ["6", "2", "4", "5", "3"];

here arrayB[0] = sum of 2s (2+2+2), arrayB[1] = sum of 1s (1+1)

I tried a lot but not able to figure out how I can achieve this.

Please help!

1
  • If arrayA were sorted, this would be a much easier problem. So can you simply sort it as a first step? Also, does arrayB have to be in any particular order? Commented May 22, 2016 at 6:42

5 Answers 5

1

You can do it this way

 arrayA = ["2", "1", "4", "2", "5", "1", "3", "2"];
 //make a set 
 var set =  new Set(arrayA);
 var arrayB = [];
 set.forEach(function(a) {
   //for each set find in the original arrayA
   var len = arrayA.filter(function(f) {
     return f == a;
   });
   //push in arrayB set element * number of it occurance in original array
   arrayB.push((+a) * (len.length));
 })
 console.log(arrayB)

Sign up to request clarification or add additional context in comments.

Comments

1

This should work:

var arrayA = ["2", "1", "4", "2", "5", "1", "3", "2"];
var tmp={};
for (var i=0; i<arrayA.length; i++)
    tmp[arrayA[i]] = (tmp[arrayA[i]]||0)+1;

var res=[];
for (var i=0; i<arrayA.length; i++)
    if(tmp[arrayA[i]]) {
        res.push(arrayA[i]*tmp[arrayA[i]]);
        delete tmp[arrayA[i]];
    }

res would be:

[6, 2, 4, 5, 3]

Comments

1

You can do it by looping a first time to sum them up, and a second time to recreate the array:

var arrayA = ["2", "1", "4", "2", "5", "1", "3", "2"];
var arrayB = sumDuplicates(arrayA);

console.log(arrayB);

function sumDuplicates(arr) {
  var obj = {}, res = [];

  for(var i = 0, l = arr.length; i < l; i++)
    obj[ arr[i] ] = (obj[ arr[i] ] || 0) + +arr[i]; // +arr[i] converts them to Numbers
  // Now, obj = {"2": 6, "1": 2, "4": 4, "5": 5, "3": 3}
  for(var i = 0, l = arr.length; i < l; i++) 
    if(arr.indexOf( arr[i] ) >= i) res.push( "" + obj[ arr[i] ] ); // Back to String

  return res;
}

Comments

1
var arrayA = ["2", "1", "4", "2", "5", "1", "3", "2"];
var arrayB = [];

for (var i = 0; i < arrayA.length; i++) {
  if (arrayA.indexOf(arrayA[i]) === i) {
    var count = 1;
    for (var j = i + 1; j < arrayA.length; j++) {
      if (arrayA[j] === arrayA[i]) {
        count++;
      }
    }
    arrayB.push(arrayA[i] * count);
  }
}

console.log(arrayB);
// [ 6, 2, 4, 5, 3 ]

Comments

1
  1. Solution using map given below:

    var arrIn = ["2", "1", "4", "2", "5", "1", "3", "2"];
    var arrOut = sumOfDupl(arrIn);
    
    console.log(arrOut);
    
    function sumOfDupl(arr) {
      var mapKey = new Map();
      var mapSum = {};
      var resultArray = [];
    
      for(var i = 0; i < arr.length; i++) {
        mapSum[arr[i]] = (+ mapSum[arr[i]])?(+ mapSum[arr[i]] + (+ arr[i])): (+ arr[i]);
        mapKey.set(arr[i], 1);
      }
    
      for (var [key, value] of mapKey) {
        resultArray.push(mapSum[key]);  
      }                  
    
      return resultArray;
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.