Implement Even Occurrence in JavaScript
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Implement Even Occurrence in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
This is the second in a series about programming interview questions and how to think through and solve them! Have you already solved this one? Check out all permutations of a set.
This article is covering the even occurrence programming interview question in JavaScript. *Find the first item that occurs an even number of times in an array. Remember to handle multiple even-occurrence items and return the first one. Return null if there are no even-occurrence items. *For example, if given the input of [1, 2, 2, 3, 4, 4, 4, 4], the output should be 2.
The Problem
The problem of implementing Even Occurrence in JavaScript involves finding the first element in an array that occurs an even number of times. To solve this problem, you need to design an algorithm that can efficiently iterate through the array and keep track of the occurrence count for each element. The algorithm should identify the first element that occurs an even number of times and return it as the result. JavaScript provides various data structures and methods that can be utilized to implement an efficient solution, such as using objects or maps to store the occurrence counts. By breaking down the problem, you can identify key steps, such as iterating through the array, updating the occurrence count for each element, and checking for even occurrences. The solution to this problem enables JavaScript applications to easily identify elements with even occurrences, allowing for further processing or analysis of data.
Analysis
My first thought was to use an object to store each value as a key, then associate it with the count of each value. From there, we know we can’t just loop through the object because an object doesn’t keep order like an array.
We can then loop through the original array and check to see if the current value has an even number of occurrences within the object. If we find one, we’ll return that first value. If we don’t come across one, we have to return null.
Pseudocode
Loop through input Add each unique value to storage, keep count of each value Loop through input Check to see if the current character inside storage is even
if nothing, return null
Time Complexity
The time complexity of the above algorithm should be the same as the number of items produced. Therefore, with an input size of n the time complexity is linear — O(n).
Code
Nice job making it through this one! Work through the next one, all permutations of a set which is a very common one asked in advanced programming interviews.
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
I founded Higglo Digital and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a beautifully designed website, check us out.
I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!