2

I have two arrays, A = [22,33,22,33] and B = [3,10,5,9].

I want to create a new array like this C = [22,max(3,5), 33, max(10,9)]

Could someone help! Thanks in advance

6
  • 2
    What’s the logic for picking those? Commented Feb 14, 2018 at 7:52
  • does both the arrays will be of same size always and you want to take maximum at those two specific points always? Commented Feb 14, 2018 at 7:52
  • When findingequal elmnts in the array A go and search in the the array B the max corresponding to these elmts Commented Feb 14, 2018 at 7:53
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Feb 14, 2018 at 7:55
  • Yes the same size Commented Feb 14, 2018 at 7:55

1 Answer 1

3

You could group by the values of array a and take the values of b at the same index of a for grouping.

var a = [22, 33, 22, 33],
    b = [3, 10, 5, 9],
    groups = new Map(),
    result;
    
a.forEach(g => groups.set(g, -Infinity)); // prevent zero false values
b.forEach((v, i) => groups.set(a[i], Math.max(groups.get(a[i]), v)));
result = [].concat(...groups);

console.log(result);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.