# 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 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!

Can you have test cases like the following?

stray([1, 1, 1, 2, 1, 1, 2, 4, 1, 2, 2, 3, 3, 2]) // 4 is odd one out

or will it only consist of two different values like:

stray([1, 1, 3, 1, 1]) // 3 is odd one out
stray([2, 1, 1, 1, 1]) // 2 is odd one out
stray([5, 5, 5, 5, 3]) // 3 is odd one out

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!

Here is my approach without sorting the array first. I used the fact that there would only be 3 scenarios in which the odd one out would appear. The first two scenarios occur when the first and second number are not equal to each other. For example:

[1, 2, 1, 1, 1]
OR
[1, 2, 2, 2, 2]

All I had to do was compare the second number to third number and if they are equal, then first number has to be to odd one out, If they are not equal, then the second number is the odd one out.

If the first two numbers are equal, then starting with the third number, you just need to find the first element which is different from the first number.

Here was my approach:

function stray([firstNum, secondNum, ...numbers]) {
if (firstNum !== secondNum) {
const thirdNum = numbers[0];
return secondNum === thirdNum ? firstNum : secondNum;
}
for (var nextNum of numbers) {
if (firstNum !== nextNum) {
return nextNum;
}
}
}

This solution also handles the edge case where there is only one number. When there is only one number, the first scenario applies, because the first number will not be equal to the second number (which is undefined because there is no second number) and the second number is equal to the third number (which is also undefined because it too does not exist either).

1 Like

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?

If you want to know the number of times a specific number appears in an array, then you will need to pass the specific number and the array to the function. How else would the function be able to tell you how many times that specific number appears?

Are you still talking about an array that only has only two possible numbers like this particular challenge had?

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…

What if the array had multiple numbers or strings which appeared the most but also the same number of times? What value(s) would you want to return and in what data structure?

If it is guaranteed to only be a single element value which appears the most, then this problem can be solved in one pass of the array. If more than one element value could appear at the same highest frequency, then it is a different problem.

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!

If there is guaranteed to be only one element value which is the most frequent, then you could do something like:

function singleMode(arr) {
const seen = {};
let mostFreqVal= {val: null, count: 0};
for (let val of arr) {
seen[val] = seen[val] ? seen[val] + 1 : 1;
if (seen[val] > mostFreqVal.count) {
mostFreqVal= {val, count: seen[val]};
}
}
return mostFreqVal.val;
}

singleMode([1,1,2,3,3,1,4,1,4]) // returns 1

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.

What you are asking is in Mathematics known as the Mode, the value which occurs the most frequent in a set of data.

1 Like

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!