Solution: First Non-Repeating Integer in a List—Hashing
Let’s solve the First Non-Repeating Integer in a List—Hashing problem.
Statement
Given a list of integers, nums
, find the first non-repeating integer in the list.
Constraints:
nums.length
nums[i]
Solution 1: Using a dictionary to keep count of repetitions
We track the frequency of each element in the list using a dictionary, which stores key-value pairs. The keys represent the elements of the list, and the values indicate how many times each element occurs.
Initialize an empty dictionary,
counts
, to store the counts of each element.Iterate through the list of elements.
If the element is already a key in
counts
, increment its value byto represent its count. If the element is not in
counts
, add it to the dictionary with a count of.
Iterate through the list again.
Check the count of each element in
counts
.Return the first element with a count of
.
Let’s look at the illustration below to better understand the solution:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.