Reputation: 69
I have an array having key value pair like this:
[3, "Rey"],[3,"Rhio"],[4,"Rey"],[1,"derter"];
I want to get an resulting array so that if the value is same , then key having maximum value would be taken.like below
[3,"Rhio"],[4,"Rey"],[1,"derter"];
Upvotes: 1
Views: 1294
Reputation: 2883
I will tell you the approach you can take:
Start iterating the array, keep pushing each array element to a map, where first element is the key, and second is the value. Duplicates are gone as the duplicate keys are overridden conditionally.
Then form an array yourself by iterating over the map, where each array element is an array itself with kay as first element and value as second element. All the best.
Upvotes: 1