0

Example hashmap value

3 - 2
20 - 2
4 - 2
5 - 3

How to get key value 5 because it has unique/distinct value of 3 different from other with value of 2

Im trying to looping and comparing previous value still working on this. Is there a way to get this without loop because I have loop already and I hope no loop solution to make code run faster.

UPDATE I tried doing below just removing directly and not adding in hashmap but still i get wrong result

int i = a.length - 1;
while (a.length > 1) {
  System.out.println(Arrays.toString(a));
  System.out.println(a[i]);
  System.out.println(ArrayUtils.contains(a, a[i]));
  if (ArrayUtils.contains(a, a[i])) {
    // Do some stuff.
    a = ArrayUtils.removeElement(a, a[i]);
  }
  i--;
}
guradio
  • 15,524
  • 4
  • 36
  • 57
  • Is this Java or JavaScript? Your code snippet looks like Java code, but it's enclosed in a snippet container for JavaScript, which is confusing. Also, your Java code shows an array but you're asking about a HashMap, again confusing. Anyway, to try to answer your question, no, you can't directly lookup a value in a map, that's what the key is for. And what if there are multiple entries for the given value? – k314159 Jun 20 '23 at 15:15

0 Answers0