js find duplicates in array

function getDuplicateArrayElements(arr){
var sorted_arr = arr.slice().sort();
var results = [];
for (var i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] === sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}

var colors = ["red","orange","blue","green","red","blue"];
var duplicateColors= getDuplicateArrayElements(colors);//["blue", "red"]

3.5
2
Wjandrea 105 points

                                    const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']

const count = names =&gt;
  names.reduce((a, b) =&gt; ({ ...a,
    [b]: (a[b] || 0) + 1
  }), {}) // don't forget to initialize the accumulator

const duplicates = dict =&gt;
  Object.keys(dict).filter((a) =&gt; dict[a] &gt; 1)

console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]

3.5 (2 Votes)
0
3.5
2
Mythio 120 points

                                    [1, 2, 2, 4, 3, 4].filter((e, i, a) =&gt; a.indexOf(e) !== i) // [2, 4]

3.5 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
check for repeated values in list js find duplicate elements in array Check if an array contains a duplicate number javascript get duplicated array ids javascript find duplicates in array without built in function how to check if an array has duplicate values in javascript check array for duplicate values javascript find duplicateletter in array js list duplicate numbers in array javascript find duplicates from array javascript javascript check array no duplicates find out how many duplicate elements in an array javascript find duplicate values javascript array find duplicates array javascript how to find duplicates in string javascript How to find out how many times an item is repeated in an array js find duplicates on array javascript count duplicate values in array javascript find array duplicates find a duplicate number in an array dind dublicate number in array js how to find duplicate elements in an array how to find the duplicate values in array by given value in javascript find duplicates in string javascript typescript find duplicates in array find duplicate elements in one array javascript duplicate array javascript js array duplicate check how to find the non duplicates in an array javascript js array check for duplicates find repeated values in array javascript javascript check for duplicate entries how to check if there are repeating values in an array javascript how to check duplicate string in array javascript find duplicate item in array typescript javascript find all duplicates in array find duplicates in array js search same elements in js javascript get duplicates in array find all duplicates in an array js get same value in array js js find duplicate values in array how to find duplicates in an array javascript how to check duplicate array in javascript find duplicate row in same array javascript js array find duplicates js find repeating element return all same values from an array find duplicate numbers in array javascript find same value in array javascript find duplicate array elements javascript find duplicate javascript array find duplicate in array javascript get same number in array js know repeated values from array javascript js check duplicates in array duplicate numbers in an array javascript js how to check for repeating index how to find non duplicate items in an array using js filter items in js if they are non unique findduplicates javascript nodejs find duplicate elements find duplicates in an array javascript get duplicate in array javascript get duplicates in array javascript js find duplicates in array indexof javascript array return all the recurring Use if condition and indexOf() to find duplicate items get same value of array javascript reduce js array get not the same elements javascript find duplicates in array es6 how to check duplicate element in array in javascript how to get duplicate values from array in javascript select duplicate in list of string typescript Find a duplicate value from array javascript find duplicates in array get all duplcates js js array select duplicates view duplicates javascript array find duplicate in array js how to check duplicate elements in an array more than once javascript find duplicate number in array JS javascript return repeated items from an array only once find duplicate number in array javascript find duplicates in array javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source