Check if array contains duplicate values in Javascript
In Javascript, there are many ways to check if an array contains duplicate values. One way is to use the Set data structure, which can only contain unique values. Another way is to use a for loop to iterate through the array and keep track of values that have been seen before in a separate data structure. If a value is seen more than once, then the array contains duplicates.
Solution 1: Use Set() function to check if array contains duplicate values in array
If you are working with arrays in JavaScript, you may need to check if an array contains duplicate values. One way to do this is to use the Set() function. The Set() function creates a new Set object with unique values. You can compare the size of the result from the Set() function with the array's length.
A Set is a collection of values and those values are unique. It means a value in a Set can only occur once.
Code example
// create a function for checking duplicates
function contains_duplicates(arr) {
return new Set(arr).size !== arr.length;
}
// Test on multiple arrays
console.log(contains_duplicates([10, 20, 30, 40, 10]));
// -> true
console.log(contains_duplicates([30, 50, 10, 40, 90]));
// -> false
Output
true
false
The first line creates a function that checks if an array contains duplicates. The function returns true if the array contains duplicates and false if it doesn't. The second line tests the function on two different arrays. The first array contains duplicates, so the function returns true. The second array doesn't contain duplicates, so the function returns false.
If you are using ES6 then you can use the below code
const contains_duplicates = (arr) => new Set(arr).size !== arr.length;
const names1 = ['James', 'Anderson', 'Rohit', 'James'];
console.log(contains_duplicates(names1));
// -> true
const names2 = ['James', 'Sumit', 'Sonia', 'Clark'];
console.log(contains_duplicates(names2));
// -> false
Output
true
false
Solution 2: Using For loop
If you need to check if an array contains duplicate values, the best way to do it is using a For loop. This method is simple and effective, and it will give you accurate results.
Code example
function contains_duplicates(arr) {
var vals = [];
for (var i = 0; i < arr.length; ++i) {
if (vals.indexOf(arr[i]) !== -1) {
return true;
break;
}
vals.push(arr[i]);
}
return false;
}
const arr1 = [1, 3, 2, 5, 6, 1];
if (contains_duplicates(arr1)) {
console.log("Array contains duplicate values");
} else {
console.log("Array does not contain duplicate values");
}
Output
Array contains duplicate values
The above code is checking if the given array contains duplicate values. It does this by looping through the array and adding each value it finds to a new array.
If the value is already in the new array, then it returns true, meaning the original array contains duplicate values. If the value is not already in the new array, it adds it to the new array and continues looping.
If the loop finishes without finding any duplicates, then it returns false.
Solution 3: Use some() function to check if an array contains duplicate values
The some() function is used to check if an array contains duplicate values. This is a boolean function that returns true if there is at least one duplicate value in the array, and false if there are no duplicate values.
Code example 1:
const contains_duplicates = (input_arr) => input_arr.some((e, i, arr) => arr.indexOf(e) !== i);
console.log(contains_duplicates([20, 50, 90, 70, 50, 30]));
// -> true
Output
true
Code example 2:
function contains_duplicates(input_arr) {
return input_arr.some((e, i, arr) => arr.indexOf(e) !== i)
}
const numbers = [20, 50, 90, 70, 30];
if (contains_duplicates(numbers)) {
console.log("Duplicate values found");
} else {
console.log("No duplicate values");
}
Output
No duplicate values
The code example above is checking to see if there are any duplicates within the inputted array.
The some() method is used to check if at least one element within the array passes the test implemented by the callback function. In this case, the callback function is checking if the index of the current element is not equal to the first index at which the element is found within the array.
If there is at least one element within the array that passes this test, then the contains_duplicates() function will return true.
Solution 4: Using indexOf() and lastIndexOf() functions
You can also use indexOf() and lastIndexOf() functions to check if an array has duplicate values or not.
The indexOf() function returns the first index at which a given element can be found in the array, or -1 if the element is not present in the array.
The lastIndexOf() function returns the last index at which a given element can be found in the array, or -1 if the element is not present in the array.
If the indexOf() and lastIndexOf() functions return the same index, then it means that there is only one instance of the element in the array and no duplicates.
To understand it, check the below code example
function contains_duplicates(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])) {
return true;
}
}
return false;
}
const result1 = contains_duplicates([1, 1, 3, 9, 7, 5]);
console.log(result1);
// -> true
const result2 = contains_duplicates(["Math", "Physics", "Math", "Chemistry"]);
console.log(result2);
// -> true
const result3 = contains_duplicates(["Math", "Physics", "Chemistry"]);
console.log(result3);
// -> false
Output
true
true
false
The first function, contains_duplicates(), takes in an array as a parameter. It then loops through the array to check if any of the values appear more than once. If so, it returns true. Otherwise, it returns false.
The second and third functions are just examples of how the contains_duplicates() function would work with different arrays. In the second example, the array contains duplicates, so the function returns true. In the third example, the array does not contain duplicates, so the function returns false.
- Remove duplicate values from an array in Javascript
- How to Remove duplicate elements from array in JavaScript ?
- Remove duplicate items from array in Javascript
- Join two arrays and remove duplicate elements using Javascript
- Get all values from an array which does not exist in other array Lodash
- Create fixed length array with blank values in it Javascript
- Get the sum of array values using Javascript - Solved