how to get duplicate values from array in javascript
JavaScript
var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];
console.log([...new Set(
array.filter((value, index, self) => self.indexOf(value) !== index))]
);const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
const count = names =>
names.reduce((a, b) => ({ ...a,
[b]: (a[b] || 0) + 1
}), {}) // don't forget to initialize the accumulator
const duplicates = dict =>
Object.keys(dict).filter((a) => dict[a] > 1)
console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]var input = [1, 2, 3, 1, 3, 1];
var duplicates = input.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);
document.write(duplicates); // = 1,3 (actual array == [1, 3])var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
var uniq = names
.map((name) => {
return {
count: 1,
name: name
}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
return a
}, {})
var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
console.log(duplicates) // [ 'Nancy' ]let a = [1, 2, 3, 4, 2, 2, 4, 1, 5, 6]
let b = [...new Set(a.sort().filter((o, i) => o !== undefined && a[i + 1] !== undefined && o === a[i + 1]))]
// b is now [1, 2, 4]const arr = ["q", "w", "w", "e", "i", "u", "r"]
arr.reduce((acc, cur) => {
if(acc[cur]) {
acc.duplicates.push(cur)
} else {
acc[cur] = true //anything could go here
}
}, { duplicates: [] })
Also in JavaScript:
- Title
- Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
- Category
- JavaScript
- Title
- delete from array based on value javascript
- Category
- JavaScript
- Title
- href before onclick js
- Category
- JavaScript
- Title
- get browser timezone javascript
- Category
- JavaScript
- Title
- dataset js
- Category
- JavaScript
- Title
- how to check version of node js in cmd
- Category
- JavaScript
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- how to add text to h2 with jquery
- Category
- JavaScript
- Title
- at leastone checkbox required jquery
- Category
- JavaScript
- Title
- get current screen name react navigation
- Category
- JavaScript
- Title
- format a date moment
- Category
- JavaScript
- Title
- how to create a popup in javascript
- Category
- JavaScript
- Title
- is forEach sync js
- Category
- JavaScript
- Title
- capitalize all letters jquery
- Category
- JavaScript
- Title
- add firebase angular
- Category
- JavaScript
- Title
- Javascript case insensitive string comparison
- Category
- JavaScript
- Title
- console log all array values node
- Category
- JavaScript
- Title
- add a slash to string in javascript
- Category
- JavaScript
- Title
- clone an object in javascript
- Category
- JavaScript
- Title
- array push javascript example
- Category
- JavaScript
- Title
- javascript .firstordefault
- Category
- JavaScript
- Title
- function prototype javascript
- Category
- JavaScript
- Title
- form submit programmatically
- Category
- JavaScript
- Title
- angular bootstrap not working
- Category
- JavaScript
- Title
- expo create react native app
- Category
- JavaScript
- Title
- Javascript get random item from array
- Category
- JavaScript
- Title
- * http://sizzlejs.com/
- Category
- JavaScript
- Title
- install node modules
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- get row data in datatable
- Category
- JavaScript
- Title
- how to return an object in javascript
- Category
- JavaScript
- Title
- javascript add multiple items to array
- Category
- JavaScript
- Title
- iterate through nested object javascript
- Category
- JavaScript
- Title
- javascript base64 encode
- Category
- JavaScript
- Title
- how to use jQuery hide and show to display multiple images
- Category
- JavaScript
- Title
- A cookie associated with a cross-site resource at http://placeholder.com/ was set without the `SameSite` attribute.
- Category
- JavaScript
- Title
- iterate over object javascript
- Category
- JavaScript
- Title
- columndefs in datatable not working while setting width jquery
- Category
- JavaScript
- Title
- D3.js
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- if you run a script.js with the code, how do you access the value passed to "var" inside script.js ...
- Category
- JavaScript
- Title
- display none in jquery
- Category
- JavaScript
- Title
- delete js
- Category
- JavaScript
- Title
- Javascript detect mobile browser
- Category
- JavaScript
- Title
- javascript array concat spread operator
- Category
- JavaScript
- Title
- find leap year javascript
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- How do I check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- how to get the size of the window in javascript
- Category
- JavaScript
- Title
- How to rewrite a function using arrow function syntax
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- add all elements in array javascript
- Category
- JavaScript
- Title
- get iso date javascript
- Category
- JavaScript
- Title
- ISS proxy express
- Category
- JavaScript
- Title
- foreach w3schools javascript
- Category
- JavaScript
- Title
- beautify json python
- Category
- JavaScript
- Title
- angular material toolbar with logo
- Category
- JavaScript
- Title
- get full url nodejs
- Category
- JavaScript
- Title
- custom processing datatables
- Category
- JavaScript
- Title
- copy array javascript
- Category
- JavaScript
- Title
- angular cli skip-tests
- Category
- JavaScript
- Title
- how to check if variable is array in javascript
- Category
- JavaScript
- Title
- get client timezone offset JavaScript
- Category
- JavaScript
- Title
- drupal 8 node has field
- Category
- JavaScript
- Title
- how to set text for label in jquery
- Category
- JavaScript
- Title
- fatorial recursivo em javascript
- Category
- JavaScript
- Title
- elseif javascript
- Category
- JavaScript
- Title
- iterate object js
- Category
- JavaScript
- Title
- javascript add function to onchange event
- Category
- JavaScript
- Title
- chart.js data from mysql vanila javscript
- Category
- JavaScript
- Title
- get query string javascript nodejs
- Category
- JavaScript
- Title
- javascript change meta tag
- Category
- JavaScript
- Title
- adding border in react native
- Category
- JavaScript
- Title
- how to count seconds in javascript
- Category
- JavaScript
- Title
- ionic status bar color
- Category
- JavaScript
- Title
- (function (g, d, a) {})(window, document, jQuery);
- Category
- JavaScript
- Title
- does a function inside a function need to be called?
- Category
- JavaScript
- Title
- client timezone offset in JavaScript
- Category
- JavaScript
- Title
- includes()
- Category
- JavaScript
- Title
- add option to select jquery
- Category
- JavaScript
- Title
- how to deep copy object in javascript
- Category
- JavaScript
- Title
- how to deap clone an object in javascript
- Category
- JavaScript
- Title
- how to instance in a node with code godot
- Category
- JavaScript
- Title
- function increase a number by click javascript
- Category
- JavaScript
- Title
- document queryselectorall and map javacript
- Category
- JavaScript
- Title
- "npm supertest
- Category
- JavaScript
- Title
- how to make a screen recording software with js
- Category
- JavaScript
- Title
- how to display image in html from json object
- Category
- JavaScript
- Title
- javascript check for duplicates in array
- Category
- JavaScript
- Title
- how to replace array element in javascript without mutation
- Category
- JavaScript
- Title
- floor in js
- Category
- JavaScript
- Title
- javascript check if number is hexadecimal
- Category
- JavaScript
- Title
- angular moment pipe timeAgo
- Category
- JavaScript
- Title
- javascript basic math
- Category
- JavaScript
- Title
- get keys wher value is true in object in javascript
- Category
- JavaScript
- Title
- javascript array pop
- Category
- JavaScript
- Title
- emmet jsx visual studio
- Category
- JavaScript
- Title
- $pull mongoose
- Category
- JavaScript
- Title
- fonction fleche javascript
- Category
- JavaScript
- Title
- foreach javascript
- Category
- JavaScript