Cara menggunakan find duplicate array javascript

See the Pen JavaScript - Find duplicate values in a array - array-ex- 20 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: write a JavaScript program to compute the sum of each individual index value from the given arrays.
Next: Write a JavaScript program to flatten a nested (any depth) array.

What is the difficulty level of this exercise?

Easy Medium Hard

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

Sort Number Arrays

JavaScript arrays come with a built-in sort method. This sort method converts the array elements into strings and performs a lexicographical sort on it by default. This can cause issues when sorting number arrays. Hence here is a simple solution to overcome this problem.

[0,10,4,9,123,54,1].sort((a,b) => a-b);
>>> [0, 1, 4, 9, 10, 54, 123]

You are providing a function to compare two elements in the number array to the sort method. This function helps us the receive the correct output.

In this tutorial I will show you how you can check if an Array contains duplicate elements, but also, we will look at how you can return the elements that are duplicates. Both techniques focussed around the Set data type.

Let's start with the data that will use in both examples:

const shoppingList = [
  'bread',
  'bread',
  'milk',
  'pie',
  'juice'
]

Option 1: Check if an Array contains duplicate elements

This is the easier of those two methods that I will talk about in this tutorial, and in fact, it is basically a one liner logic wise.

function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}

If you run the above code, you should have the following output:

true

This function is pretty straightforward, but I will give a quick rundown.

Basically what this function does is it takes in an array. It will then initialise a Set from that array and use the size property which gets compared to the input array's

function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
0. If the Set has fewer elements, we know that the input array contains duplicate elements.

Option 2: Find and return duplicate Array elements

The following method is more complicated than option 1, however, it is doing more than just returning whether or not an array contains duplicates as it will also return the duplicate values.

Take a look at the following code:

function findAndReturnDuplicatesInArray(list) {
  const inputList = new Set()
  const duplicates = new Set()

  for (const item of list) {
    if (inputList.has(item)) {
      duplicates.add(item)
    } else {
      inputList.add(item)
    }
  }

  return duplicates
}

If you run the above function, you should get the following output:

Set(1) { 'bread' }

What is happening here?

We initialise two Set's, one Set will be used to keep track of the elements that we have already checked, and the other Set will be used to keep track of the duplicate elements.

After that, we loop through the

function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
5 array that was passed to our function. When looping, it will check if the
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
6 Set contains the current
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
8, if the Set does contain the
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
8 then it will add that
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
8 to the
true
2 Set, if
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
6 does not contain the
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
8, then
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
8 gets added to
function doesArrayContainDuplicates(list) {
  return new Set(list).size < list.length
}
6

At the end of this function we return the

true
2 Set which will contain all the duplicate values.

Bonus: Remove duplicate elements from Array

Another great use of Set can be to remove duplicates from an Array.

To do this, all we need to do is the following:

const shoppingListSet = new Set(shoppingList)
const shoppingListArray = new Array.from(shoppingListSet)

While this is powerful, I am not a fan of using this. I find it to be a bit messy. If I did need this, I would put it in a function just so that it would be cleaner to use.

Conclusion

Finding out whether an Array contains duplicates is super easy when using Set, likewise, finding and returning the duplicate elements is quite simple too. The Set data type can be an incredibly powerful tool, one other benefit of using a set is that the

function findAndReturnDuplicatesInArray(list) {
  const inputList = new Set()
  const duplicates = new Set()

  for (const item of list) {
    if (inputList.has(item)) {
      duplicates.add(item)
    } else {
      inputList.add(item)
    }
  }

  return duplicates
}
4 function could be much faster than the
function findAndReturnDuplicatesInArray(list) {
  const inputList = new Set()
  const duplicates = new Set()

  for (const item of list) {
    if (inputList.has(item)) {
      duplicates.add(item)
    } else {
      inputList.add(item)
    }
  }

  return duplicates
}
5 function on the Array data type, depending on the amount of data you have.

If you found this article helpful, please consider sharing it with others that might also find it helpful.