JavaScript Get all non-unique values from an array
We have given a JavaScript array, and the task is to find all non-unique elements of the array. To get all non-unique values from the array here are a few examples.
These are the following methods to get all non-unique values from an array:
Table of Content
- Using Array Slice() Method
- Using for loop
- Using filter() Method
- Using indexOf() Method
- Using reduce() and an object to count occurrences:
Using Array Slice() Method
This method returns the selected elements in a new array object. This method gets the elements starting from the specified start argument and ends at excluding the given end argument.
Syntax:
array.slice(start, end)
Parameters:
- start: This parameter is optional. It specifies the integer from where to start the selection (index starts from 0). Negative numbers are used to select from the end of the array. If not used, it acts like “0”.
- end: This parameter is optional. It specifies the integer from where the end of the selection. If not used all elements from the start to the end of the array will be selected. Negative numbers are used to select from the end of the array.
Example: This example first sorts the array and then selects them one by one if they are non-unique.
let arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7];
let sort_arr = arr.slice().sort();
let res = [];
function gfg_Run() {
for (let i = 0; i < sort_arr.length - 1; i++) {
if (sort_arr[i + 1] == sort_arr[i]) {
res.push(sort_arr[i]);
}
}
console.log("Non Unique values are: " + res);
}
gfg_Run();
Output
Non Unique values are: 2,4,89
Using for loop
In this method make a dictionary and then store the frequency of each element. Later if they are found to be a frequency of more than 1, then the element will be selected.
Example:
let arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7];
function gfg_Run() {
let len = arr.length,
output = [],
count = {};
for (let i = 0; i < len; i++) {
let item = arr[i];
count[item] = count[item] >= 1 ? count[item] + 1 : 1;
if (count[item] === 2) {
output.push(item);
}
}
console.log("Non Unique Values: " + output);
}
gfg_Run();
Output
Non Unique Values: 89,2,4
Using filter() Method
Example: This example uses the filter method which checks the index of elements varies for elements any element and returns elements.
let arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7];
function gfg_Run() {
let output = arr.filter(
(item, index) => arr.indexOf(item) !== index);
console.log("Non-Unique values are = " + output);
}
gfg_Run();
Output
Non-Unique values are = 89,2,4
Using indexOf() Method
Here we iterate through the array and for each element we check if its first occurrence index (indexOf) is different from its last occurrence index (lastIndexOf) in the array. If they are different, it means the element appears more than once in the array, so we add it to the non-unique values array.
Example:
function getNonUniqueValues(arr) {
const nonUniqueValues = new Set();
for (let i = 0; i < arr.length; i++) {
const firstIndex = arr.indexOf(arr[i]);
const lastIndex = arr.lastIndexOf(arr[i]);
if (firstIndex !== lastIndex) {
nonUniqueValues.add(arr[i]);
}
}
return Array.from(nonUniqueValues);
}
// Example usage
const array = [1, 2, 2, 3, 4, 5, 5, 6];
console.log(getNonUniqueValues(array)); // Output: [2, 5]
Output
[ 2, 5 ]
Using reduce() and an object to count occurrences
Using reduce() and an object to count occurrences involves initializing an empty object to store counts. The array is iterated with reduce(), updating counts in the object. Non-unique values are collected based on their count
Example: In this example we use reduce() and an object to count occurrences of array elements, capturing non-unique values when their count reaches 2.
let array = [1, 2, 3, 2, 4, 5, 1];
let count = {};
let nonUniqueValues = array.reduce((acc, value) => {
count[value] = (count[value] || 0) + 1;
if (count[value] === 2) acc.push(value);
return acc;
}, []);
console.log(nonUniqueValues);
Output
[ 2, 1 ]
Contact Us