Counting the Occurrences of Integers in a List

This problem asks you to write a function that takes a list of integers and returns a dictionary with the integers as keys and the number of times they appear in the list as values.

Problem

Given a list of integers, return a dictionary where the key is the integer and the value is the number of times it appears in the list.
Example input: [1, 2, 1, 3, 2, 4, 3, 5]
Example output: {1: 2, 2: 2, 3: 2, 4: 1, 5: 1}

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def count_list(lst):
    
    This function takes a list of integers and returns a dictionary where the key is the integer and the value is the number of times it appears in the list.
    
    # create an empty dictionary
    d = {}
    # loop through the list
    for i in lst:
        # if the integer is not in the dictionary, add it as a key and set the value to 1
        if i not in d:
            d[i] = 1
        # if the integer is in the dictionary, increment the value by 1
        else:
            d[i] += 1
    # return the dictionary
    return d

# test the function
print(count_list([1, 2, 1, 3, 2, 4, 3, 5]))

A.I. Evaluation of the Solution

The candidate's solution correctly solves the problem and is reasonably complete. The candidate uses a dictionary to keep track of the number of times each integer appears in the list, which is a good approach.

Evaluated at: 2022-11-23 14:16:08