-3

const arr = [12, 12, 10, 11];
function repeatedItem(i) {
  if (arr.indexOf(i) !== -1) return true;
  else return false
}
console.log(repeatedItem(10));
this code will return true if item does exist in array but it should be true when item was duplicated

what should do to return true if item was duplicated

Siavash_Sk
  • 35
  • 5

1 Answers1

1

const arr = [12, 12, 10, 11];

function repeatedItem(item) {
    let count = 0;

    for(let i=0;i<arr.length;i++){
        if(arr[i] === item){
            count++;
            if(count > 1){
                return true;
            }
        }
    }

    return false;
}
console.log(repeatedItem(10));
Sawan Patodia
  • 773
  • 5
  • 13