Warning: session_start(): open(/tmp/sess_4e15dcda124e0da48cbb3f5d181cd0ac, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If An Array Contains Duplicates In JavaScript - LearnShareIT

How To Check If An Array Contains Duplicates In JavaScript

Check if an Array contains Duplicates in JavaScript

This article will show you how to check if an array contains duplicates in JavaScript. Here are some ways to do it with some examples to make it easier for readers to understand. Read this article now.

Check if an array contains duplicates in JavaScript

Before coming to how to check if an array contains duplicates in JavaScript, we must understand what array contains duplicates and what array not contains duplicates.

Code:

// Array does not contain Duplicates
var arr = [ 1, 2, 3, 4, 5 ];

// Array contains Duplicates
var arr = [ 1, 2, 3, 4, 1 ];

As you can see, array contains duplicates which means that one or more elements of the array have copies of it in the array. So how do we know if the array has the same values? Check out the methods below.

Use create new Set method

Set in JavaScript is an object type used to store data without duplicates.

The methods of Set are:

  • new Set(iterable): initializes Set by passing an iterable object (optional). In case no parameters are passed, the Set will be empty.
  • set. add(value): adds a valuable element to the Set and returns the Set itself.
  • set. delete(value): deletes an element in the Set and returns true if the value exists in the Set, otherwise returns false.
  • set. has(value): returns true if the value exists in the Set. Otherwise, it returns false.
  • set.clear(): deletes all elements in the Set.
  • set. size: returns the number of elements in the Set.

We use Set because of its zero-copy property in its array to create a Set from the given array. Then use the Set. size method to get the length of the Set object and compare it with the length of the original array.

Code:

var array = [ 1, 2, 3, 4, 1 ];

// Compare set size with array length
if(new Set(array).size === array.length){
    console.log("No duplicates exist");
}else{
    console.log("Array contains Duplicates");
}

Output:

Array contains Duplicates

In the above code, because the newly created Set will filter out the same values ​​in the array, its length will be smaller and different from the length of the original array, so we can know the array contains duplicates.

Use Array.some() method 

In this article, we will learn the some function in JavaScript. This is a function used to check whether the elements in an array satisfy a particular condition or not.

Syntax:

some((element, index, array) => { ... } )

Parameters:

  • element: is the variable that holds the value of the element iterating.
  • index: is the key of the repeating element.
  • array: is the original array that the element belongs to.

In this way, we use the some() method of array to check if only one of the array elements is the same. The return value will be accurate.

Code:

var array = [ 1, 2, 3, 4, 1 ];

// Check if array exists arr.indexOf(element) different from current index
var check=array.some((element, index, arr) => arr.indexOf(element) !== index);
if(check===true){
  console.log("Array contains Duplicates");
}else{
  console.log("No duplicates exist");
}

Output:

Array contains Duplicates

In the above code, we also use the indexOf() method of the array. It will return the first index of the element in the array. 

Syntax:

string_name.indexOf(substring, start_position)

Parameters:

  • substring: is the element to be searched for in the string. This parameter is required.
  • start_position: is optional. Start_position is the position in the string that the indexOf() function starts looking for.

So when comparing it with the current index, if it is different, it means that there are two identical elements in the array. I hope these methods are helpful to you.

Summary

To summarize, the are some ways to check if an array contains duplicates in JavaScript. In this article, we have shown you how to do it by using create a new Set method or using Array. some() method. Let’s try these methods. Good luck for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *