function that duplicates data in array js

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))]
);

3.86
7
Awgiedawgie 440215 points

                                    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, 4, 111, 2, 3, 9, 4, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);

3.86 (7 Votes)
0
3
1
Awgiedawgie 440215 points

                                    function doubleValues(array) {
  var newArray = [];
  array.forEach(function (el) { newArray.push(el, el); });    
  return newArray;
}

console.log(doubleValues([1,2,3]));

3 (1 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
How do you find the duplicate number on a given integer array? javascript js duplicate an element count duplicate values in array javascript create duplicate element of an array in js duplicate method in javascript js check for duplicates in array duplicate array function duplicate items in array ja javascript filter duplicates in array how to find duplicates in array javascript duplicate data javascript js count duplicates in array js filter duplicates from array duplicates count in array jhs how to duplicate a number in js how to duplicate array method to check for duplicates java script duplicate item js duplicate javascript filter duplicates in array javascript check array for duplicates js how to duplicate each elements of array in javascript how to filter duplicate valus from a array in javascript duplicate array javascript array have duplicate items? duplicate free array in js how to filter duplicates in an array javascript find and print duplicates in javascript array use array with duplicates array duplicate javascript check for if a value is duplicate in an array javascript extract array duplicates js array duplicates in javascript javascript array duplicate duplicate element in array duplicate elements in array contains duplicate an array js [...] duplicate an array js count duplicate elements in array javascript how to find repeated element in array js how to check data in same array in javascript which is repeated how to find dupllicated in an array javascript find duplicate values in array javascript duplicate values in foreach javascript duplicate values in javascript foreach for loop array duplicated elements javascript how to compare two elements of array in javascript searchin duplicates how to duplicate in javascript how to duplicate elements in js how to duplicate array in javascript best way to duplicate array js js duplicate array duplicate an array JS check for duplications in array duplicate elements in array how to check if there are duplicates in an array javascript make duplicate array in javascript duplicates js duplicate list elements javascript duplicate values in array finding duplicates in array javascript duplicate array js in array to find duplicate vlalue and print in javascript duplicate value check in list js duplicates elements in that array how to count duplicates in an array javascript javascript for loop array duplicate elements duplicate elements array how to store duplicates in js javascript count duplicate numbers in array js find duplicates in array
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