Practice Question for certification!

Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order.

Note: The extra space is only for the array to be returned.
Try and perform all operations within the provided array.
Example 1:

Input: N = 4 a[] = {0,3,1,2} Output: -1 Explanation: There is no repeating element in the array.Therefore output is -1.

Example 2:

Input: N = 5 a[] = {2,3,1,2,3} Output: 2 3 Explanation: 2 and 3 occur more than once in the given array.
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner. If no such element is found, return list containing [-1].
Try to do that in class method or using function!!

3 Likes

function duplicates(a, N) {
const duplicatesMap = {};

for (let i = 0; i < N; i++) {
    const element = a[i];

    if (duplicatesMap[element] === undefined) {
        duplicatesMap[element] = 1;
    } else {
        duplicatesMap[element]++;
    }
}

const duplicatesList = Object.keys(duplicatesMap)
    .filter((element) => duplicatesMap[element] > 1)
    .map(Number)
    .sort((a, b) => a - b);

return duplicatesList.length > 0 ? duplicatesList : [-1];

}
const N2 = 5;
const a2 = [2, 3, 1, 2, 3];
console.log(duplicates(a2, N2));

1 Like

Function Method:

<!DOCTYPE html>
<html>
<head>
  <title>Find Duplicates</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin: 30px;
    }

    h2 {
      color: #009688;
    }

    label {
      font-weight: bold;
    }

    input {
      padding: 5px;
      margin: 10px 0;
    }

    button {
      background-color: #009688;
      color: white;
      padding: 10px 20px;
      border: none;
      cursor: pointer;
    }

    button:hover {
      background-color: #00796B;
    }

    p {
      margin-top: 20px;
      font-weight: bold;
      color: #009688;
    }
  </style>
</head>
<body>

<h2>Find Duplicates in Array</h2>

<script>
function findDuplicates(arr) {
  const n = arr.length;
  const duplicates = [];

  for (let i = 0; i < n; i++) {
    const index = Math.abs(arr[i]);
    if (arr[index] < 0) {
      duplicates.push(index);
    } else {
      arr[index] = -arr[index];
    }
  }

  return duplicates.length === 0 ? [-1] : duplicates.sort((a, b) => a - b);
}

function displayDuplicates() {
  const inputArray = document.getElementById('inputArray').value.trim();
  const array = inputArray.split(',').map(Number);

  const result = findDuplicates(array);
  const outputElement = document.getElementById('output');

  outputElement.innerText = 'Output: ' + (result[0] === -1 ? '[-1]' : result.join(', '));
}
</script>

<label for="inputArray">Enter the array elements (comma-separated):</label>
<input type="text" id="inputArray">
<button onclick="displayDuplicates()">Find Duplicates</button>

<p id="output"></p>

</body>
</html>

1 Like

Class Method:

<!DOCTYPE html>
<html>
<head>
  <title>Find Duplicates</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin: 30px;
      background: linear-gradient(45deg, #00bcd4, #009688);
    }

    h2 {
      color: #fff;
    }

    label {
      font-weight: bold;
      color: #fff;
    }

    input {
      padding: 5px;
      margin: 10px 0;
    }

    button {
      background-color: #009688;
      color: white;
      padding: 10px 20px;
      border: none;
      cursor: pointer;
    }

    button:hover {
      background-color: #00796B;
    }

    .output-container {
      margin-top: 20px;
    }

    .output-text {
      font-weight: bold;
      color: #fff;
      margin-top: 10px;
    }

    .no-duplicates {
      color: #F44336;
    }
  </style>
</head>
<body>

<h2>Find Duplicates in Array</h2>

<script>
class DuplicatesFinder {
  findDuplicates(arr) {
    const n = arr.length;
    const duplicates = [];

    for (let i = 0; i < n; i++) {
      const index = Math.abs(arr[i]);
      if (arr[index] < 0) {
        duplicates.push(index);
      } else {
        arr[index] = -arr[index];
      }
    }

    return duplicates.length === 0 ? [-1] : duplicates.sort((a, b) => a - b);
  }
}

function displayDuplicates() {
  const inputArray = document.getElementById('inputArray').value.trim();
  const array = inputArray.split(',').map(Number);

  const finder = new DuplicatesFinder();
  const result = finder.findDuplicates(array);
  const outputElement = document.getElementById('output');

  outputElement.innerHTML = `<p class="output-text">${result[0] === -1 ? '[-1]' : 'Duplicate elements: ' + result.join(', ')}</p>`;
  outputElement.classList.add(result[0] === -1 ? 'no-duplicates' : 'duplicates-found');
}
</script>

<label for="inputArray" style="color: #fff;">Enter the array elements (comma-separated):</label>
<input type="text" id="inputArray">
<button onclick="displayDuplicates()">Find Duplicates</button>

<div class="output-container" id="output"></div>

</body>
</html>

1 Like

let res=[];
function duplicate(arr){
  arr.sort();
  for(let i=0;i<arr.length-1;i++){
    if(arr[i]==arr[i+1]){
      if(res.includes(arr[i])){
        continue;
      }
      res.push(arr[i]);
    }
  }
  if(res.length==0){
  return res.push(-1);
}
else{
  return res;
}

}

let arr=[2,3,5,6,8];
duplicate(arr)

console.log(res)

1 Like