1

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"];
4
  • 5
    So what have you tried yourself? Nothing? Anything? Commented Dec 27, 2012 at 13:06
  • possible duplicate stackoverflow.com/questions/10301184/… Commented Dec 27, 2012 at 13:09
  • 3
    @laxonline The "duplicate" is PHP, this is JavaScript. Commented Dec 27, 2012 at 13:09
  • possible duplicate : stackoverflow.com/a/9191823/734181 Commented Dec 27, 2012 at 13:11

1 Answer 1

1

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.

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

2 Comments

LPD Here i want the removal of value having lowest key number
@ParekhShah, Yes. I understand. Write the logic accordingly and that should do. Isn't it?

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.