DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#22 - Find the unique number CodeWars Kata (6 kyu)

Instructions

There is an array with some numbers. All numbers are equal except for one. Try to find it!

For example:

findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55

It’s guaranteed that array contains at least 3 numbers.


My solution:

function findUniq(arr) {
  let repeated = arr.filter((item, index) => arr.indexOf(item) !== index)
  return arr.filter((item)=> item !== repeated[0])[0]
}
Enter fullscreen mode Exit fullscreen mode

Explanation

First I made an array that contained the values that were repeated , I filtered the array leaving only the items that their index in the array isn't equal to the index being iterated, so I get the repeated elements.

let repeated = arr.filter((item, index) => arr.indexOf(item) !== index)

Input --> [ 1, 1, 1, 2, 1, 1 ]
Output --> [ 1, 1, 1, 1 ]


After that I filtered the initial array again, and checked if the ietm being iterated is equal to the first element of the repeated array, and after that I returned the first value of that array, because it returned the result inside of an array.

return arr.filter((item)=> item !== repeated[0])[0]

Input --> [ 1, 1, 1, 2, 1, 1 ]
Output --> [ 2 ] --> 2


What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

Discussion (6)

Collapse
pengeszikra profile image
Peter Vivo • Edited on
const getUnique = (list, sorted = [...list].sort()) => 
   sorted.find(
     (item, index) => 
        item !== sorted?.[index + 1] 
        && item !== sorted?.[index - 1]
   );

const test = [111,111,-1,-2,3,4,5,-5,5];

getUnique(test); // -1

getUnique([]); // undefined

getUnique([2,2]); // undefined
Enter fullscreen mode Exit fullscreen mode
Collapse
cesar__dlr profile image
Cesar Del rio Author

Thanks for the comment bro 🔥, I understand what you want to do, you'll check every item and then check the element next to it and the element behind it so you know if they're equal, but when you test it, remember that the problem gives you an array with only 2 numbers (the repeated one and the unique one), you are testing it with a lot of different numbers, so it returns the first number that is different to the 2 next to it

111, -1 , -2

I don't know if this would work with the last element of the array, because index+1 doesn't exist, or with the first one because the element index-1 doesn't exist.

Either way I think it's a great solution, you just need to adapt it a little bit more to the problem 🙌

Keep going!! 💪

Collapse
lexlohr profile image
Alex Lohr • Edited on

You can use the array methods find and lastIndexOf/indexOf to do this in a more direct manner:

return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))
Enter fullscreen mode Exit fullscreen mode

find has the added advantage over filter that it stops iterating after the first match. Sorting the array will only work for numbers, strings or objects that can be coerced to either.

Collapse
cesar__dlr profile image
Cesar Del rio Author

Great solution for optimizing the code, thanks bro! 🙌🔥

Collapse
jonrandy profile image
Jon Randy • Edited on
const findUniq = (a,s=a.sort())=>s[s[1]-s[0]?0:s.length-1]
Enter fullscreen mode Exit fullscreen mode

Sort the array, check if first two elements are equal - if they are, unique is last element, otherwise it's the first element

Collapse
cesar__dlr profile image
Cesar Del rio Author

Makes sense bro, great solution! 🙌