How to Return Recurring Elements in a Javascript Array With O(n) Time Complexity

We are a community of developers, designers, and marketers. We are journaling every experience and step we take in our pursuit for growth and nurturing.
Search for a command to run...

We are a community of developers, designers, and marketers. We are journaling every experience and step we take in our pursuit for growth and nurturing.
No comments yet. Be the first to comment.
As a tech professional, you probably spend most of your day sitting in front of a computer. While this may be great for your career, it's not so great for your health. But don't worry, there's a simple solution: EXERCISE! Recently I've been going to ...

In today's rapidly evolving world, staying ahead requires constant upskilling and innovation. The Renaissance Innovation Labs or RILearn Bootcamp stands as a beacon of opportunity, offering comprehensive skill development that goes beyond traditional...

Introduction In the face of the current economic downturn affecting various industries, including the tech sector, companies and individuals must adapt and find ways to thrive in these challenging times. In this blog post, we will discuss effective s...

Introduction Are you a freelancer, entrepreneur, or remote worker looking for a lively coworking space in Port Harcourt, Nigeria to boost your productivity and develop a sense of community? Look no further than Ubuntu Space, a thriving and innovative...
In the past few years, technology has made significant progress, and it's now striving to establish gender balance. There is a concerted effort to explore technology's potential to enable women to live their lives with the same freedom and opportunit...

Hello there!
In this article, I’ll be showing you how to return recurring elements in a Javascript Array WIth O(n) Time complexity.
First, I’ll be showing you the wrong way to do it, then the correct way.
Let’s consider the given array;
const numbers = [1, 3, 2, 1, 2, 3, 4, 5, 6, 4];
const findDuplicates = (arr) => {
const duplicates = [];
arr.forEach((number, index) => {
if (index !== arr.lastIndexOf(number)) {
duplicates.push(number);
}
});
return duplicates;
};
//findDuplicates(numbers) will return [ 1, 2, 3, 4, 6 ]
In as much as this is the quickest method one can think of, under the hood has a nested loop which in turn returns an O(n)^2 time complexity.
So we move on to method 2.
In the second method, we’ll be using something called a hashMap. A hashMap is a data structure that allows us to store key-value pairs and we can use non-primitive datatype to store the data.
The Map will return the key and value pairs in the same order we inserted and it retrieves this in O(n) time-complexity.
So our second solution will look like this;
const findDuplicates = (arr) => {
const duplicates = [];
const map = new Map();
arr.forEach((number) => {
if (map.get(number)) {
duplicates.push(number);
} else {
map.set(number, number);
}
});
return duplicates;
};
//findDuplicates(numbers) will return [ 1, 2, 3, 4, 6 ]
I do hope you’ve found this helpful, Thanks.