DEV Community

Christopher Coffee
Christopher Coffee

Posted on

Leetcode: Single Number (Kotlin)

Single Number is an easy question on Leetcode. I will discuss the problem and my solution below.
Single Number - LeetCode

Problem Statement

Examples

Constraints

Brainstorm

The solution I thought of is pretty straightforward.

  1. They give us an array of integers, so we can use a map that will store each number as the key and the count of that number as the value.

  2. We iterate through the array to set each number’s count in the map.

  3. We will then iterate through the array again, but this time we will check each number’s count in the map and return the number with the count of one

They guarantee a number with a count of one is in the given array, so you could technically return any number at the end because it should never get to that point. I use Int.MIN_VALUE.

Solution

Top comments (0)