1
function findOdd(A) {
  var distinctElements = [...new Set(A)];
  distinctElements.forEach(x => {
    var count = A.filter(y => y === x).length;
    if (count % 2 != 0) {
      return x;
    }
  });
}

I don't understand why program is still executing after return statement.

epascarello
  • 204,599
  • 20
  • 195
  • 236
A Manoj
  • 21
  • 5

1 Answers1

1

Your return statement is being executed within a scope.

Try replacing the function with:

function findOdd(A) {
    var distinctElements = [...new Set(A)];
    for (x of distinctElements) {
        var count = A.filter(y => y === x).length;
        if (count % 2 != 0) {
            return x;
        }
    }
}
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
  • You should `return` out of the `forEach` after assigning a value to `out` to maintain the behavior of the original program. – Aplet123 Mar 28 '20 at 13:18