Two Distinct Elements [Edabit]

Edabit

In each input array, every number repeats at least once, except for two. Write a function that returns the two unique numbers.

Examples

returnUnique([1, 9, 8, 8, 7, 6, 1, 6]) ➞ [9, 7]

returnUnique([5, 5, 2, 4, 4, 4, 9, 9, 9, 1]) ➞ [2, 1]

returnUnique([9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]) ➞ [5, 6]

Notes

Keep the same ordering in the output.

Solution:

function returnUnique(arr) {
  let sol = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr.lastIndexOf(arr[i]) === arr.indexOf(arr[i])) {
      sol.push(arr[i]);
    }
  }
  return sol;
}

Leave a comment