Remove duplicates in an array

Hello all :slight_smile: I’m back with another question!

If I have an array (category) that have multiple of the same values (retail for example)

How do I remove duplicates and just return just one (Retail, Technology, Auto etc)

So it just leaves data like this:

Before:

category: “Retail, Retail, Retail, Technology”,

After:

category: “Retail, Technology”

I have code example here:

Thank you,

John.

What code have you tried so far?

This is a common problem in real life scenarios, so there’s a lot to learn from this, and my personal opinion is that you should come up with your own implementation for a deeper learning, and then compare ideas after.

As always a bit of pseudo-code goes a long way :slight_smile:

1 Like

I’ve tried the following:

const categories = "Retail, Retail, Retail, Technology";

const listCategories = [...new Set(categories.split(/,\s?/))];

Or alternative:
const listCategories = [...new Set(categories.match(/[A-z]+/g))];


const result = listCategories.join(", ");
console.log({result})

But can not get it to work with the main array in json. Help please!! Cheers :slight_smile:

See when I do it with const of categories it works:

but when I try and do it with a .map to access the array it doesn’t that is where I am at :slight_smile:

Well, array.map returns an Array, so when you declare

const companyCat = companies.map(function(company) {
  return company.category;
});

this is the output, an array of strings.

["Finance, Finance, Technology, Auto", "Retail, Technology", "Auto, Auto, Auto", "Retail, Retail, Retail, Technology", "Technology, Technology", "Finance, Technology"]

Then CodeSandbox correctly proceed to inform you that there’s no split method existing for array…
since split is a string method.

Maybe you wanted to call split on each entry of companyCat array?

1 Like

So maybe I should be using a forEach loop to call split on each of the companyCat entries? Thank you for the awesome help :wink:

Maybe something like this:

companies.forEach(function(category) {
  const newCategory = [...new Set(category.match(/[A-z]+/g))]
  console.log(newCategory);
});

I am not sure hence why I am asking for help!

I’ve updated this:


But get errors! Is there a guide I can read about this? Thank you :slight_smile:

Hello there,

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

Specifically, I would suggest you spend some time going over the documentation for these methods:

  1. forEach
  2. map
  3. includes
  4. pop

You can achieve your goal using some of the above. There is no reason to use all of them, but any combination might be helpful. Read the documentation to take full advantage of each method.

Hope this helps

1 Like

Hi Sky020,

Thanks for this, yes I am doing FCC. I have the first certificate and am doing the JS cert now but am struggling with the JS cert.

This code problem came into my head and wanted to try and solve it but have issues, ok back to the JS lessons again :slight_smile:
Cheers!

Do not fret about struggling with the JavaScript. Even the fCC curriculum is no where near enough, by itself. It provides an excellent pathway through topics, but you will always have to research for yourself.

Keep up with it. It is good that you are trying to solve an idea.

1 Like

Thank you :slight_smile: