about | help | code help+videos | done | prefs |
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}.
// 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) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Copyright Nick Parlante 2017 - privacy