0

Every one I am new to javaScript and Solving some exercises but i got stacked to Check whether a given array of integers contains particular element twice. I can find duplicate using filter but not able to find solution with particular elements like If I want to check whether a array is having 12 and 13 twice or more. Here is my code to find duplicate.

function checkTwice() {
let arr = [12,65,31,26,13,51,26,12,51]

let newArray = arr.filter((value,index,array)=> {

 return array.indexOf(value) === index

 })
 console.log(newArray)
 }

 checkTwice()
  • Does this answer your question? [Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array](https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array) – Alon Eitan Jun 10 '20 at 12:58
  • whats the expected output? – bill.gates Jun 10 '20 at 12:59

4 Answers4

0
function checkTwice(theNum) {
    const arr = [12,65,31,26,13,51,26,12,51];
    const newArray = arr.filter( x => x === theNum);
    return theNum + ' found ' + newArray.length + ' times!';
} 

console.log ( checkTwice ( 12 ) );

This will tell you exactly how many times a given arg is found in the array. If you only want to know if a number is found exactly twice (and not say, once or three times etc), just do this:

return (newArray.length === 2 ? theNum + ' found twice!' : theNum + ' not found twice, it was found ' + newArray.length + ' times!';

If you need to do this without feeding the function an arg, and just want to list out the numbers in the array that appear twice or more (again, if only twice, the modification would be the same as above):

function checkTwice() {
    const arr = [12,65,31,26,13,51,26,12,51];
    const found = [];
    arr.forEach ( x => { 
         const test = arr.filter ( y => x === y );
         if (test.length > 1) { 
             found.push ( x );
         }
     });
    return ( found.length ? 'The following numbers were found more than once: ' + found.join( ',' ) : 'No numbers in the array were found more than once.');
} 

Following up on your comment, if you want to check one array against another:

const arrayToCheck = [12,15];
function checkTwice(arrayToCheck) {
    const arr = [12,65,31,26,13,51,26,12,51];
    const found = [];
    arrayToCheck.forEach( x => {
         const test = arr.filter( y => x === y);
         if ( test.length > 1 ) {
             found.push ( x );
         }
     });
    return ( found.length ? 'The following numbers were found more than once: ' + found.join( ',' ) : 'No numbers in the array were found more than once.');
} 
Tim Consolazio
  • 4,802
  • 2
  • 19
  • 28
  • hey @Tim Consolazio and @Anurag Gupta Thanks for your reply I have tried your solution and it works but the problem is the if I am checking it with an array then it doesn't work Like with an array `let theNum = [12,15]`. Hope you both got my point. – Akash Jangra Jun 10 '20 at 13:25
  • Alright, you didn't mention that before. So you want to check one array against another, and if any of the numbers in the array you feed into the function match the the array in the function more than once, you want to know it. I will modify the above to demo that. – Tim Consolazio Jun 10 '20 at 13:26
0

Your question isn't clear. I assume you want to count frequency of a number, say, val.

function checkTwice(val) {
let arr = [12,65,31,26,13,51,26,12,51]

let newArray = arr.filter((item) => {
    return item == val
});
console.log(newArray); // You can check the length of newArray and proceed as per your logic

}

 checkTwice(12)
Anurag
  • 353
  • 3
  • 15
  • Also, check [this question](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) to get more ideas if you meant something else. – Anurag Jun 10 '20 at 13:13
0

//Hope this will help

checkTwice = arr => {
    const sortedArr = arr.sort();
    const newArr = [];
    for (let iterator = 0; iterator < sortedArr.length - 1; iterator++) {
        if (sortedArr[iterator] === sortedArr[iterator + 1]) {
            newArr.push(sortedArr[iterator]);
        }
    }
    console.log(newArr);
}

checkTwice([12,65,31,26,13,51,26,12,51]);
Subhadeep
  • 149
  • 5
0

You can have like this

function checkTwice() {
let arr = [12,65,31,26,13,51,26,12,51]

let newArray = [...new Set(arr)].map((value , index)=>
 {
   let countApp=arr.filter(str => str === value).length;
    return countApp>1?[value,countApp]:0
 }
 );
 console.log(newArray.filter(x=>x!=0))
 }

 checkTwice()