find repeated number in array code example

Example 1: how to get duplicate values from array in 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))]
);

Example 2: find duplicates and their count in an array javascript

var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });

Example 3: 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])

Example 4: find duplicate in an array using xor

int DuplicateNumber(int arr[], int size){
    int ans=0;
    for(int i=0;i<size;i++){
        ans= ans ^ arr[i] ; 
    }
    for(int i=0;i<=size-2;i++){
        ans= ans ^ i;
    }
    return ans;
   }

Example 5: how to count duplicates in an array javascript

arr.reduce((b,c)=>((b[b.findIndex(d=>d.el===c)]||b[b.push({el:c,count:0})-1]).count++,b),[]);

Example 6: find duplicate in an array using xor

int DuplicateNumber(int arr[], int size){
    /* Don't write main().
     * Don't read input, it is passed as function argument.
     * Return output and don't print it.
     * Taking input and printing output is handled automatically.
     */
    
    int ans=0;
    for(int i=0;i<size;i++){
        ans= ans ^ arr[i] ;
       
    }
    for(int i=0;i<=size-2;i++){
        ans= ans ^ i;
       
    }
    return ans;
    

}

Tags:

Java Example