Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

Easy-Peasy: How to Get All Non-Unique Values in an Array

Are you tired of manually checking for duplicate values in a JavaScript array? 🔄

Well, fret no more! 🛡️

In this blog post, we'll uncover the easiest way to detect and retrieve all those pesky non-unique values in your array 🕵️‍♀️ without the hassle of index-finding or tallying duplicate counts.

So, let's dive right in and get our array-game on! 💪

The Context

Our curious reader posed this intriguing question: "What's the easiest way to find duplicated values in a JavaScript array without the need for index information or counts?"

Good news! The JavaScript gurus have got you covered like a cozy array blanket. 🛌💤

The Solution 🌟

Introducing the Array.filter() method! 🆒🔍

This magical function allows us to create a new array with only the elements that pass a given test. And conveniently, with this test, we can easily identify non-unique values. 🎉

Here's the snippet to bring smiles to your array-detecting dreams:

const array = [1, 2, 3, 4, 1, 3, 5, 6];

const nonUniqueValues = array.filter((value, index, self) => self.indexOf(value) !== index);

console.log(nonUniqueValues); // [1, 3]

And voilà! 🎩

After applying the .filter() method along with a handy comparison of the index of each value, we swiftly retrieve all those duplicate entries. No indexes, no counting, just the non-unique values you've longed for! 😍

But wait, there's more! 📣

In the event that you have an array of objects or complex data structures, worry not my friend! 🚀

With a slight modification to our solution, you can conquer the arrays of objects too! 🏆

Here's an example using objects as array elements:

const people = [
  { name: 'John', age: 20 },
  { name: 'Jane', age: 30 },
  { name: 'John', age: 40 },
  { name: 'Jim', age: 20 },
];

const nonUniquePeople = people.filter((person, index, self) => 
  self.findIndex((p) => p.name === person.name) !== index
);

console.log(nonUniquePeople); 
// [
//   { name: 'John', age: 20 },
//   { name: 'John', age: 40 },
// ]

Boom! 💥

By combining .filter() with .findIndex(), we defeat the mighty object arrays too. We specifically target the duplicate objects based on a particular property, in this case, the name. Flex that array power! 💪

One Last Thing... 🛎️

Learning is great, but utilizing knowledge is even better! 💡

Now that you have the power to easily identify non-unique values in arrays, go forth and create amazing things! 🌟 Share your newfound wisdom, inspire others, and remember, there's always something new to explore in the realm of coding. 🚀

So, let's hear from you! Have you ever encountered a tricky array situation, and if so, how did you resolve it? Share your stories, tips, and tricks in the comments below, and let's level up together! 👇💬

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my