-5

Hi i need a javascript program which finds the element in the given array of integers which is placed in the array right before the first repeating element.

In this way, for the following example input:

-1 2 5 6 2 9 -1 6 5 6 -1 3

The output should come as 9

In the same way when input is :- 4 2 6 2 5 4

output has to be 6

3
  • 8
    java !== javascript Commented Mar 23, 2018 at 7:22
  • 1
    please add what you have tried. Commented Mar 23, 2018 at 7:22
  • 3
    Stack Overflow is not a free code-writing service. If it were, I'd be unemployed. Commented Mar 23, 2018 at 7:23

1 Answer 1

-3

You can use array.prototype.find, array.prototype.indexOf and array.prototype.lastIndexOf:

var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, 6, -1, 3];
var res = arr.find(e => arr.indexOf(e) === arr.lastIndexOf(e));
console.log(res);

Sign up to request clarification or add additional context in comments.

3 Comments

Is this Java or Javascript? Should you not wait for OP to clarify?
I don't think this can help the OP (obviously, copy-and-paste code won't help you be better at programming) or others (there is already a dupe).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.