Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find duplicates in array javascript

let arr = [1, 2, 3, 4, 5, 5];
const seen = new Set();
const duplicates = arr.filter(n => seen.size === seen.add(n).size);
console.log(duplicates); // [ 5 ]
console.log(duplicates.length); // 1
Comment

count duplicate elements in array javascript

const counts = {};
const sampleArray = ["1", "5", "9", "14", "5", "22", "48", "25", "22", "20", "9" ,"13"]
sampleArray.forEach(function (x) { counts[x] = (counts[x] || 0) + 1; });
console.log(counts)
 // output: {
  '1': 1,
  '5': 2,
  '9': 2,
  '13': 1,
  '14': 1,
  '20': 1,
  '22': 2,
  '25': 1,
  '48': 1
}
Comment

find duplicate values in array javascript

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

check for duplicates in array javascript

[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true

[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
Comment

how to get duplicate values from array in javascript

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

how to find duplicates in an array

const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const set = new Set(numbers);

const duplicates = numbers.filter(item => {
    if (set.has(item)) {
        set.delete(item);
    } else {
        return item;
    }
});

console.log(duplicates);
// [ 2, 5 ]
Comment

how to get duplicate values from array in javascript

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' ]
Comment

Find duplicate or repeat elements in js array

function findUniq(arr) {
  return arr.find(n => arr.indexOf(n) === arr.lastIndexOf(n));
}

console.log(findUniq([ 0, 1, 0 ]))
console.log(findUniq([ 1, 1, 1, 2, 1, 1 ]))
console.log(findUniq([ 3, 10, 3, 3, 3 ]))
console.log(findUniq([ 7, 7, 7, 20, 7, 7, 7 ]))
Comment

find duplicates in array

function findDuplicates(arr) {
	const duplicates = new Set()
  
  return arr.filter(item => {
  	if (duplicates.has(item)) {
    	return true
    }
    duplicates.add(item)
    return false
  })
}
Comment

how to get duplicate values from array in javascript

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: [] })
Comment

javascript check for duplicates in array

function checkIfDuplicateExists(w){
    return new Set(w).size !== w.length 
}

console.log(
    checkIfDuplicateExists(["a", "b", "c", "a"])
// true
);

console.log(
    checkIfDuplicateExists(["a", "b", "c"]))
//false
Comment

find duplicate values in array javascript

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)}`);
Comment

find duplicate element on array

const order = ["apple", "banana", "orange", "banana", "apple", "banana"];

const result = order.reduce(function (prevVal, item) {
    if (!prevVal[item]) {
        // if an object doesn't have a key yet, it means it wasn't repeated before
        prevVal[item] = 1;
    } else {
        // increase the number of repetitions by 1
        prevVal[item] += 1;
    }

    // and return the changed object
    return prevVal;
}, {}); // The initial value is an empty object.

console.log(result); // { apple: 2, banana: 3, orange: 1 } 
Comment

how to get duplicate values from array in javascript

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]
Comment

Find / Display duplicate numbers in array

var input = [1, 2, 5, 5, 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;
    }, []);
    console.log(duplicates);
Comment

find-array-duplicates

npm i find-array-duplicates

import duplicates from 'find-array-duplicates'

const names = [
 { 'age': 36, 'name': 'Bob' },
 { 'age': 40, 'name': 'Harry' },
 { 'age': 1,  'name': 'Bob' }
]
 
duplicates(names, 'name').single()
// => { 'age': 36, 'name': 'Bob' }
Comment

find array is duplicate or not

  const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

let isDuplicate = false;

// Outer for loop
for (let i = 0; i < numbers.length; i++) {
    // Inner for loop
    for (let j = 0; j < numbers.length; j++) {
        // Skip self comparison
        if (i !== j) {
            // Check for duplicate
            if (numbers[i] === numbers[j]) {
                isDuplicate = true;
                // Terminate inner loop
                break;
            }
        }
        // Terminate outer loop
        if (isDuplicate) {
            break;
        }
    }
}

if (!isDuplicate) {
    console.log(`Array doesn't contain duplicates.`);
} else {
    console.log(`Array contains duplicates.`);
}
// Output: Array contains duplicates.
Comment

duplicate array values

public class Main {
public static void main(String[] args) {
  int[] arr1 = {1,2,3,3,4};
  for (int i = 0; i < arr1.length-1 ; i++) {
    for (int j = 0; j < arr1.length; j++) {
      if (arr1[i]==arr1[j] && (j != i)){
      System.out.println("array's duplicate number: " +
      arr1[j]);
      	}
      }
    }
	}
}
Comment

js find duplicate value

duplicate items
Comment

array iteration + find duplicates

function lonelyinteger(a: number[]): number {
   for (let item of a) {
        const items = a.filter(el => item === el)
        if (items.length === 1) {
            return item
        }
    }
}



const a:number[] = [1,2,3,4,3,2,1]

const result:number = lonelyinteger(a);
console.log(result)
Comment

find duplicates array javascript

let arr = [1, 7, 8, 9, 10, 20, 33, 0, 20, 7, 1]
console.log([... new Set(arr)]
// Output : [1, 7, 8, 9, 10, 20, 33, 0]
Comment

PREVIOUS NEXT
Code Example
Javascript :: sanitize html in javascript 
Javascript :: is javascript faster than python 
Javascript :: overflowy javascript 
Javascript :: what are closures 
Javascript :: round to 2 decimal places 
Javascript :: array includes javascript 
Javascript :: what is asynchronous 
Javascript :: .shift javascript 
Javascript :: use moment js in ejs file 
Javascript :: simple node rest 
Javascript :: javascript regex One or more occurrences of the pattern 
Javascript :: html form data to json 
Javascript :: best reactjs course on udemy 
Javascript :: what triggers formik validate 
Javascript :: react 17 
Javascript :: emitting event on socket.io using async await 
Javascript :: node js split 
Javascript :: Check propery of an objects array 
Javascript :: JSX Conditionals: && 
Javascript :: javascript tostring 
Javascript :: array -1 javascript 
Javascript :: javascript loops 
Javascript :: discord js if no arguments 
Javascript :: Object.Values () javascript 
Javascript :: js content editable 
Javascript :: trigger a button click with javascript on the enter key in a text box 
Javascript :: GET method firebase realtime database react 
Javascript :: javascript check if length is greater than 0 
Javascript :: js slice string at word 
Javascript :: nds npm 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =