Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than O(n2):
const findDuplicates = (arr) => {
let sorted_arr = arr.slice().sort(); // You can define the comparing function here.
// JS by default uses a crappy string compare.
// (we use slice to clone the array so the
// original array won't be modified)
let results = [];
for (let i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than O(n2):
const findDuplicates = (arr) => {
let sorted_arr = arr.slice().sort(); // You can define the comparing function here.
// JS by default uses a crappy string compare.
// (we use slice to clone the array so the
// original array won't be modified)
let results = [];
for (let i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
Sorting an array, having all non-unique values nested
Have a look at this one-liner. Guess this is what you need.
Note that this isn't the most efficient way to to this - but guess it's beautyfull!
const array = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
const newArray = [...new Set(array)].sort((a, b) => a-b).map(e => new Array(array.filter( i => i === e).length).fill(e)).map(e => e.length === 1 ? e[0] : e);
console.log(newArray);
Get all unique values in a JavaScript array (remove duplicates)
With JavaScript 1.6 / ECMAScript 5 you can use the native filter
method of an Array in the following way to get an array with unique values:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
Related Topics
Convert Form Data to JavaScript Object With Jquery
How to Detect If a Browser Window Is Not Currently Active
How to Create a Two Dimensional Array in JavaScript
Deleting Array Elements in JavaScript - Delete VS Splice
What Is a Good Regular Expression to Match a Url
Chrome/Firefox Console.Log Always Appends a Line Saying 'Undefined'
"Uncaught Syntaxerror: Cannot Use Import Statement Outside a Module" When Importing Ecmascript 6
Checking If a Key Exists in a JavaScript Object
Get a Random Item from a JavaScript Array
Google Maps Js API V3 - Simple Multiple Marker Example
How to Set/Unset a Cookie With Jquery
Understanding Unique Keys For Array Children in React.Js
How to Disable Scrolling Temporarily
JavaScript Equivalent of Python'S Zip Function
Can't Append ≪Script≫ Element