We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static int lonelyinteger(List<Integer> a) {
// Write your code here
// Create a hashmap to store the frequency of each element
HashMap<Integer, Integer> frequencyMap = new HashMap<>();
// Populate the hashmap with element frequencies
for (int num : a) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
// Find and return the element with a frequency of 1
for (int key : frequencyMap.keySet()) {
if (frequencyMap.get(key) == 1) {
return key;
}
}
return -1;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
JAVA code using hashmap
}