Athletes who did not finish the race (with.Java)
This article looks into the “Athletes who did not finish the race (with.Java)” issue.
As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.
Let’s look at the problem first.
problem
Numerous marathon runners participated in the marathon.
All but one athlete completed the marathon.
When given an array participant containing the names of athletes who participated in the marathon and an array completion containing the names of athletes who completed the marathon, write a solution function to return the names of athletes who did not complete the marathon.
Restrictions
- The number of athletes participating in the marathon race is between 1 and 100,000.
- The length of completion is 1 less than the length of the participant.
- The participant’s name must consist of at least 1 and not more than 20 lowercase letters.
- Among the participants, there may be people with the same name.
Input/Output Example
participant | completion | return |
---|---|---|
[“leo”, “kiki”, “eden”] | [“eden”, “kiki”] | “leo” |
[“marina”, “josipa”, “nikola”, “vinko”, “filipa”] | [“josipa”, “filipa”, “marina”, “nikola”] | “vinko” |
[“mislav”, “stanko”, “mislav”, “ana”] | [“stanko”, “ana”, “mislav”] | “mislav” |
My solution to the problem
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
HashMap<String, Integer> map = new HashMap<>();
for(String temp1 : participant){
map.put(temp1, map.getOrDefault(temp1, 0) + 1);
}
for(String temp2 : completion){
map.put(temp2, map.get(temp2) - 1);
}
for(String key : map.keySet()){
if(map.get(key) > 0){
answer = key;
}
}
return answer;
}
}
Solved review
The solution method receives a participant array and a completion array as input.
We will initialize the answer variable, which will store the name of the missing participant.
Creates a HashMap<String, Integer> object map. Where String is the participant’s name, and Integer represents the number of occurrences of that name.
In the first for loop, it iterates over each element of the participant array, uses the name as a key in the map, and stores the number of occurrences of that name as the value. If the name already exists in the map, the number of appearances is increased.
The second for loop iterates through each element of the completion array and decrements the number of occurrences of that name if it exists in the map.
The third for loop iterates through all keys in the map, looking for occurrences greater than 0, i.e. names of participants who did not finish. Save this name as answer.
Finally, it returns an answer.
In other words, this code is used to solve the problem of finding missing participants by comparing the names of participants and finishers.