id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

firstAppearance


Complete the firstAppearanceProblem method so that it takes an array of strings and returns a Map that maps from the strings in the array to the index of their first appearance. For example, the array {"A", "B", "A", "B", "C"} would result in a map containing {"A":0, "B":1, "C":4}.

Since CodingBat doesn't allow Map return values, you will need to copy the following two methods into the code window. The first method converts your HashMap to a string to allow output checking. You need to complete the second method. Click on "Show Hint" to seen an abbreviated version of the Map API.

// This method just calls your code and formats the results
// for CodingBat. Don't change it!
public String firstAppearance(String[] words) {
   Map<String, Integer> map = firstAppearanceProblem(words);
   Object[] keys = map.keySet().toArray();
   Arrays.sort(keys);
   String result = "{";
   for (int i = 0; i < keys.length; i++) {
     String word = (String)keys[i];
     result += word + ":" + map.get(word);
     if (i < keys.length-1) {
        result += ", ";
     } 
   }
   return result + "}";
}

public Map<String, Integer> firstAppearanceProblem(String[] words) {
  // YOUR CODE HERE!
}

firstAppearance(["one"]) → "{one:0}"
firstAppearance(["one", "one"]) → "{one:0}"
firstAppearance(["car", "car", "car", "hat"]) → "{car:0, hat:3}"

...Save, Compile, Run (ctrl-enter)

public String firstAppearance(String[] words) { // Replace this with the code above! }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Post-solution available

Copyright Nick Parlante 2017 - privacy