Return the odd one out in an array

I’m trying to do a codewars by returning the odd one out in a function. This passes most tests except for one, I’m assuming I’ve missed a step and it doesn’t know what to do if the first number in the sequence is the odd one out?

function stray(numbers) {
let odd = numbers[0];
for (var i = 1; i < numbers.length; i++) {
if (numbers[i] !== numbers[0]) {
odd = numbers[i];
}
}
  return odd;
}

What this function is supposed to do? Return first odd number, all of them, their indexes?

Please provide more details and where it fails.

At the moment I can see that your function is comparing every number (starting from index 1) with first number (index 0) and assign it to odd variable when they are different.

Hi, it’s supposed to take the odd one out of an array of numbers and return it.
I think the code has gone wrong because I tried to build on a previous programme I wrote that would return true if the numbers in an array we’re all the same, and false if they weren’t, but I’m not sure that will help here…

it’s supposed to take the odd one out of an array of numbers and return it

You mean it has to return first odd number in the array?

Anyhow you can check if number is odd or even by using modulus %.

4 % 2     // returns 0
9 % 2     // returns 1

Sorry, the odd one out, e.g. [2, 1, 1, 1] would return 2. Each array passed through is assumed to only have one number that’s different.

OK :slight_smile: now I understand. I guess there are plenty of ways for finding unique values in an array. For example one of the way would be counting up all the occurences and return one that occure only once.
Second would be comparing indexOf and lastIndexOf - unique value should have the same in both.

Thanks, I may try those in future but I couldn’t quite get the code to work for all cases. In the end I went with sorting them and then returning the first or last value in the array by comparing the first two!
I’d really like to get the hang of keeping count of number of occurances of a value for other purposes, do you have any hints for how I could have gone about it for this challenge?
Thank you!

Hi, there would only be two values, with one of them being unique.
I started trying with a for loop but got a bit lost and ended up doing sort, but it would be good to know the set up for this in case I need to find out how many times a value appears in an array!

Thanks everyone!
Does anybody know if this would be possible to solve by counting the number times a value appears but with a function that only takes an an array as an argument?
All the examples I see online can return the number of times a value appears if that is passed into the function, but what if I wanted to return the values that appear only once, or the value that appears the most in an array for example?

Hi so I’m wondering if it’s possible to have an array like:

[1,1,2,3,3,1,4,1,4]

and then if I wanted to find the value that appeared the most amount of times, it would return 1, and the function would work on an indeterminate range of values and length of array. A function that would work on strings also?

Just wondering if this is possible. If it is it might be too advanced for me…

Considering these questions, I guess what I’m after isn’t really necessary as you would usually have either the value passed in, or more knowledge about the array itself…

The reply from wawraf seemed to suggest one way to solve this problem was to count the number of times something appears, but this probably was because I hadn’t explained that the problem I was trying to solve only takes an array with two values as an argument and not a value.

Thanks anyway!

Thank you so much! I’ll have to bookmark this for a future date when I’m further along my coding journey to understand what’s going on, but it’s good to know it can be done if the array has one value with the most occurrences.

Thanks, I haven’t studied maths since I was 16 and had forgotten all about mode. It would have been helpful to use that during my google searches!