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!
arrayAwere sorted, this would be a much easier problem. So can you simply sort it as a first step? Also, doesarrayBhave to be in any particular order?