Weekly Coding Challenge: Find the odd int – written in Python

Posted by

2 min read ~ Hello readers! It’s really important for me to continue my practice and learning. Every week, I block off 30 minutes, set a timer and choose a challenge from codewars. This helps me keep my skills sharp, see how other devs all over the planet would solve these problems, and take some leisure time to problem solve and be creative. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. Today, the problem set focuses on Python.

Given an array of integers, find the one that appears an odd number of times.

There will always be only one integer that appears an odd number of times.


[7] should return 7, because it occurs 1 time (which is odd).
[0] should return 0, because it occurs 1 time (which is odd).
[1,1,2] should return 2, because it occurs 1 time (which is odd).
[0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).
[1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time (which is odd).

def find_it(seq):
    frequency_dict = {}
    odd_int = None
    for s in seq:
        frequency_dict[s] += 1 if s in frequency_dict else frequency_dict[s] = 1
    for key, value in frequency_dict.items():
        if (value % 2) != 0:
            odd_int = key
    return odd_int

First, I establish the collection variables I know that I will need based on my design and approach to solving the problem. For this one, I will need a dictionary to track the frequency of integer occurrences and a final variable to hold the return result after we qualify it. Next, I iterate over the sequence of integers and construct a map to track the frequency of numbers through the iteration. If the number exists within the dictionary, I increment a counter in the map or simply establish a counter if no match has yet been found. Finally, after I have my final map, I can simply iterate over the map items and use a modulus to extract the odd integer from the map and return it.

All solutions are also now on my GitHub profile if you want to check out all of the coding challenges I’ve completed so far!

All credit to problem sets goes to codewars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s