Start learning free

LESSON 137 OF 678 · DICTIONARIES - CHALLENGES

Count a Vote in a Swift Dictionary with default:

Practice Read the full explanation

You get votes, a Dictionary from names to how many votes each person has. You also get the name on a new vote.

Count the new vote and return votes.

The person called name gets one more vote. A name with no votes yet isn't in votes. It starts from 0, so after this vote it has 1. The order of the entries doesn't matter.

 

Example 1:

Input: votes = {"Ana": 3, "Ben": 5}, name = "Ana"
Output: {"Ana": 4, "Ben": 5}
Explanation: Ana had 3 votes. Now she has 3 + 1 = 4.

Example 2:

Input: votes = {"Ana": 3, "Ben": 5}, name = "Cara"
Output: {"Ana": 3, "Ben": 5, "Cara": 1}
Explanation: Cara had no votes yet. She starts from 0 and now has 1.

Example 3:

Input: votes = {}, name = "Max"
Output: {"Max": 1}
Explanation: This is the very first vote. Max now has 1.

 

Constraints:

  • votes can be empty.
  • Every count in votes is at least 1.
  • Names are single words made of English letters.
STEP 1 OF 4
    Ctrl + Enter to check

    SWIFT · DICTIONARIES - CHALLENGES

    Count A Vote in Swift, explained

    Problem overview

    The method countVote receives votes, a dictionary from names to how many votes each person has, and the name on a new vote. It counts the vote and returns the dictionary. The person called name gets one more vote. A name with no votes yet isn't in votes: it starts from 0, so after this vote it has 1. The order of the entries doesn't matter.

    With Ana at 3 and Ben at 5, a vote for Ana gives Ana at 4. A vote for Cara, who isn't in the dictionary, gives Cara at 1. In an empty dictionary, the first vote for Max gives Max at 1.

    Approach

    The new count is "the old count plus one", where the old count of a missing name is 0. The default: subscript says exactly that: result[name, default: 0] gives the stored count, or 0 when the name has no entry. Adding 1 and storing the result covers both cases with no if.

    Step by step

    1. Copy the parameter into a variable: var result = votes. Parameters are constants in Swift, and assigning a dictionary copies it.
    2. Read the current count into count with result[name, default: 0]. The result is a plain Int, not an optional.
    3. Set result[name] = count + 1. For a new name this adds the key. For an existing name it replaces the old count.
    4. Return result.

    The solution in Swift

    class Solution {
        func countVote(_ votes: [String: Int], _ name: String) -> [String: Int] {
            var result = votes
            let count = result[name, default: 0]
            result[name] = count + 1
            return result
        }
    }
    

    Printing each result as sorted key: value pairs gives:

    Ana: 4, Ben: 5
    Ana: 3, Ben: 5, Cara: 1
    Max: 1
    

    Language details

    • A plain result[name] would be an Int?, and result[name] + 1 would not compile. The default turns the optional into a plain number.
    • Reading with a default doesn't add the key. The key appears only at the assignment.
    • The two lines can be shortened to result[name, default: 0] += 1. It does the same read, add and store in one step, and it works because the subscript with a default can be written to as well. The lesson splits it so each step is visible.

    Edge cases and common mistakes

    • Using ! on the lookup. result[name]! crashes the program for a new name, such as Cara.
    • Assigning 1 always. That would reset Ana to 1 instead of 4.
    • Forgetting to store. Computing count + 1 without writing it back leaves the dictionary unchanged.
    • Changing the parameter. Writing to votes directly doesn't compile. Use the copy.

    Think of the dictionary as a tally sheet: a person with no marks yet has no line on it, and the first mark creates the line. That is why a missing name and the number 0 mean the same thing here.

    Testing

    • Two votes in a row for the same new name: calling the method once on an empty dictionary and then again on its result, both for "Zoe", gives Zoe: 2.
    • A vote for Ben in ["Ana": 3, "Ben": 5, "Cleo": 1] gives Ana: 3, Ben: 6, Cleo: 1. Only Ben's count changes.

    The one-line form gives the same results: starting from Ana at 3, r["Ana", default: 0] += 1 and r["Cara", default: 0] += 1 end with Ana: 4, Cara: 1.

    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free