6 kyu
Find the unique number
53,782 of 153,262isqua
Loading description...
Fundamentals
Algorithms
Arrays
Performance
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
need a fresh perspective Translation Dart
This comment has been hidden.
Your solution has a bug which fails in some specific scenarios:
Some time ago I created a browser extension for Codewars which tries to help confused users and explain them, in a spoiler-free way, why their solution does not pass tests. Maybe you would find it helpful?
Additionally, if you want to post code and keep indentation, you need to use code blocks.
What a suprise seeing you again hobovsky. Thanks for the explanation, but im struggling on how else to identify that it is the odd one out.
I would greatly recommend joining Codewars Discord and its
#help-solve
channel, because it is much easier to discuss all problems there. But generally your issue boils down to: if the first element is different from the second element, how would you tell which one of the two is an outlier?This comment has been hidden.
In what language?
Yes, you made a mistake. You print more than one test case in a single line and all of them look to you as a single, large test.
Add this at the beginning of your function:
echo "\nNew test\n";
Im getting that "Number too long" error (timeout). Printing the input to check whats going on, it seems one of the input arrays contains this: 1125899906842624 1125899906842624 1125899906842624 1125899906842624 ...etc (and keeps going to the inf). This is when solving with python, in my case, at least.
It doesn't keep going to infinity. You can only print out 1.5 MiB of text to the console before it cuts you off.
Is this possible in python? The most efficient way I can think of doing this is using count() to count how many times an item appears in the array, and it's always failing at the "Very big number" test. (My code is only 4 lines long lol)
Yes, it is possible, and you have to think of some more efficient way. Hint: if there is 100 ones and a single zero, your method checks that there is 100 ones a hundred times. It is enough to check that there is 100 ones only once.
This is irrelevant.
Ok, thanks
hello, yea it's possible, I just have made it
I wrote the exact same thing as your solution (just a variable name is different) What is going on here?
If you look closely you see that there is a little but important difference in what you both loop over.
This comment has been hidden.
That method is not available in numpy version 1.24, which is the version that codewars currently uses. You could request a module update on github, but that will likely take a long time. Realistically, you just need to find another way to solve the kata. Your solution can work with minimal changes if you examine the documentation that you linked more closely.
THX for your quick reply!
Ok, that helps a bit. Another solution already completed
THX again
Java: attempt, test 2, might have more than 1 unique numbers, expecting <7.0> was <8.0>, not sure though, code works for all other tests
I cannot check your issue and fix the kata without knowing your failing code, sorry.
This comment has been hidden.
Your solution has a bug, it returns wrong answer for this array:
[1, 1, 1, 2]
.It is not a kata issue.
Ok I see the issue thank you!
This comment has been hidden.
That's a problem with your code, not a kata issue.
Simple doesn't mean performant.
"It’s guaranteed that array contains at least 3 numbers" i thought it was 3 different numbers i realize this while play game lol and now my solution is over kill (not a complain btw)
Actually very helpful for me. I have picked up bad habbits and I'm still getting used to optimzing my code. So this actually caused me to go read up on how to do this more efficentily rather than taking a "Eh it works" approach.
Difficulty varies too much on Code Wars. This one was too easy for 6kyu I think.
TEST - all tests green,
ATTEMPT - The last item | 8 should equal 7 | 3 should equal 2 | 2 should equal 1 ???
Add Performance tag
Done.
this was my easiest 6 kyu
Execution Timed Out (12000 ms) Are you fkn joking ? I have only 9 lines of code author delete this kata dont be ridiculous
What makes you think that 9 lines of code has anything to do with how fast the code is? Apparently, your 9 lines is too slow. You need a solution which is fast, even if it has 50 lines.
Ok thanks, but you should at least tell about it before.
That's already in the kata's description.
This comment has been hidden.
Your solution has a bug, it fails following test case:
[TestCase(new [] {2, 1, 2}, ExpectedResult = 1)]
.While you are right that not presenting inputs is not a good thing, you can still
Console.WriteLine
them yourself. You owuld see that your solution fails for arrays where the unique number is on the second position.Bugs in your solution are not a kata issue.
Absolutely, I see the bug now. Thank you for pointing this out. However, the instruction containing 0.55 when code only accepts ints is misleading.
This comment has been hidden.
We don't know.
Your code fails for such cases because it breaks out of the loop and returns the first number in array.
Also, you should not only use one variable to compare whether one has encountered more than one occurrence of such number before. Otheriwse, it will fail for such tests whereby the unique number is the first element in the array.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#print-input and this: https://docs.codewars.com/training/troubleshooting#post-discourse
Your code isn't right and it fails one of the sample tests, discover which one and you'll see why.
This comment has been hidden.
How would your code know if the unique is the first item or the second one?
Yeah, that was me just being stipid. Thank you that helped alot.
This comment has been hidden.
It means you didn't return anything, when you should've returned 42.
I only took a quick look, but treating an array as string looks like a bad idea IMO. Stick to something closer to arrays/lists.
This comment has been hidden.
The solution is beautiful, but creating a dictionary probably takes a long time. The problem can be solved by looking through all the elements of arr.
What a good kata ı've ever seen!
This comment has been hidden.
This comment has been hidden.
True, but for the purposes of this task, we don't need more than 2 ;)
I keep running into this test issue when attempting to submit:
main.swift:41:7: error: conflicting arguments to generic parameter 'T' ('Double' vs. 'Int') XCTAssertEqual(findUniq(generateTestArray(225, 230, Int(220))), 225) ^ main.swift:44:31: error: cannot convert value of type '[Int]' to expected argument type '[Double]' XCTAssertEqual(findUniq(generateTestArray(-1, 1, 1000)) , -1) ^ main.swift:44:31: note: arguments to generic parameter 'Element' ('Int' and 'Double') are expected to be equal XCTAssertEqual(findUniq(generateTestArray(-1, 1, 1000)) , -1) ^ main.swift:47:31: error: cannot convert value of type '[Int]' to expected argument type '[Double]' XCTAssertEqual(findUniq(generateTestArray(42, 24, Int(223))),42) ^ main.swift:47:31: note: arguments to generic parameter 'Element' ('Int' and 'Double') are expected to be equal XCTAssertEqual(findUniq(generateTestArray(42, 24, Int(223))),42) ^ main.swift:56:9: error: conflicting arguments to generic parameter 'T' ('Double' vs. 'Int') XCTAssertEqual(findUniq(generateTestArray(a, b, 1000)), a) ^
It seems that you've changed the signature of the function, haven;t you? Originally the signature was using
Int
, but you changed it toDouble
?I am getting a failed test on this for the RandomTest where by the expected even number is not the actual even number. Given that the array is supplied and should only ever have one outlier how is this possible?
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting and mark your post as having spoiler content next time. About your problem: read the error message and see the second example in the description and you'll have your answer.
max buffer size reached how do i solve this issue
can anyone tell me the meaning of this error Test Results: Fixed_Test should_return_the_unique_number Wrong answer for 8 elements! Expected: equal to 0 Actual: 1
my code passed random test but shoe this test failed what does this error mean
Read this: https://docs.codewars.com/training/troubleshooting#print-input
JavaScript: RangeError: Maximum call stack size exceeded
This comment has been hidden.
This comment has been hidden.
I have " out of time " issue too!
This comment has been hidden.
Your solution returns good answers, but its too slow.
Solution being slow is not a kata issue.
I'm quite sad to hear that, but thanks for your reply. Any suggestions on how I could learn to approach the more difficult problems? - maybe a book suggestion or something else? :) (I want to master C++ mainly, but in the long run I want to be good at other languages too.)
No need no book to solve this kata. Just think. Maybe take a pen and a paper, how would you solve this mentally? Don't you find the answer in a glance when you consider a possible input? You don't need to start over at the beginning of the array for each element, do you? How can you code this?
Thanks, but I didn't just mean this specific kata. I meant any harder or the hardest problems.
Hi! I'm pretty new to this site, so hoping I'm posting this in the correct place. I'm trying to figure out why I'm able to get all the correct outputs when I enter this code into the console, but I'm not getting any to pass in on this kata. I've tried the 3 test cases in the console, plus multiple ones I came up with, which are all coming back correct in the console. However, in this kata, I just keep getting "expect +0 to equal 1", but even console logging in the kata shows the correct output. Is there something I'm missing, or is it a possible glitch at the moment?
Your solution fails with the input
[0, 0, 1]
. You can print the input yourself to confirm this. See https://docs.codewars.com/training/training-example/#debugging-a-kata.NunoOliveira, could you elaborate on how the unique value is still expected to be zero even when there are multiple of those values?
In the error message
Your code returned
+0
and the expected value was1
.My error is slightly different ("expected 1 to equal +0"). Maybe i'm not framing my question correctly. Can you explain why assert.strictEqual(findUniq([0, 0, 1 ]), 0) should equal 0 and not 1? My code is returning 1 which I understand but how is this not correct?
If that's a sample test, you've edited it. Backup your code, click Reset to restore them, paste your code and try again. These are the first three sample tests in javascript:
i think this is too easy for kyu 6
finished the code in python, all the tests are passed and yet it still shows an error "The first item 1 should equal 0" what does that mean?
That your code doesn't pass all tests:
Read this: https://docs.codewars.com/training/troubleshooting#print-input
thanks you helped me a lot
what does it mean the last item?
The last element of the array/list.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
If you have a particular question you'd like addressed, please be clear about what you're asking and use the
question
tag.My code is a code using NSCountedSet, but it timed out. Considering performance, I counted the count after removing duplicates, but the timeout occurs.
The "issue" tag is for problems with the kata, not problems you're having solving the kata. Please use the "question" tag if you want help with your code.
A couple of hints for solving this kata:
[8, 2, 8, 8, 8, 8, ..., 8]
how many elements would you need to see before returning the answer? There are ways to solve this without counting anything at all, and (usually) without iterating the whole array.When I implement a counting solution (not using NSCountedSet) it succeeds in about 5000ms, but if I consider the second hint my solution instead takes 600ms. Think about it.
Scala translation
Didn't he say "All numbers are equal except for one"? then why there are some test cases that has multiple values?
Probably you're printing the input of more than one test and confusing that with a single test input.
Yeah, you're correct. After adding new line after each test case, it really isn't.
This comment has been hidden.
I don't know if you found your issue since then but the issue is due to the variable you compare your value to. Try to visualize it in your head, take this array [8,8,8,8,7] and use your comparison in your head or even on paper to clearly see your mistake. You're nearly there my dude, I believe in you !
This comment has been hidden.
same problem here, if you've solved it please lead me to the right track
Step back and think from an algorithmic perspective. Your current solution scans the entire array for every element in the array, it's running in
O(n^2)
time which is why it times out.It is possible to solve this problem in linear time. Pretend I have the array, and you have a pen and paper. I am going to show you each element of the array exactly once, and your job is to find the odd one out while writing down as few things as possible on your paper. How might you do it? Solve that question and you have solved this puzzle.
This kata has no test cases for sorted array. So some solutions as i can see in case of [1, 1, 3, 8, 8] works with result of 8
Link to any of those solutions?
https://www.codewars.com/kata/reviews/585d85846aa5e61d580004cd/groups/591cefe59a24a9a39f000e65
[1, 1, 3, 8, 8] is invalid input.
Closing then.
One line solution for this one in php and i get: 'Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67108872 bytes) in /workspace/default/_solution.php on line 2' message. ¿Can anyone help me to figure out what's happening?
Hi, when I test the code it says all tests passed - successfull.
When I try to attempt I get a time out error with one test failed - "very big number".
Do you think that the mistake comes from my end or is something wrong with this kata?
Thanks.
Yes, it comes from your code being too slow. Not a kata issue.
This comment has been hidden.
Does your code work just fine in your IRB for example tests?
Yes exactly, I haven't tried with larger arrays (which I probably should have)
This comment has been hidden.
HI, don't post as an
issue
unless you can demonstrate a provable problem with the kata. Otherwise, you may post as aquestion
. Read the error messages to help you figure out what tests your code does not pass.It says expected 3.0 but it was 4.0, im not able to get it Please help!
OP solved it, closing
cool kata!
Thanks, good kata!
This kata of 6kyu took me 3 minutes to solve, whereas I know some 8kyu katas that had taken hours!
Same. This is by no means a 6kyu skill level challenge. The base solution here is a core fundamental javascript concept.
I don't get how we are supposed to deal with the larger size of array either I timed out due to large size even after slicing or I obtain a different value when I seperate into small chunks. Is there a preferable function or method to treat the array
This comment has been hidden.
This comment has been hidden.
Thank you very much! Next time i gonna format code
Times out in big number
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The error message tells you why:
Not a kata issue.
expected undefined to equal 1.
Have no idea what kind of test i didn't pass.
What should it check?.
Wrote this to check and still recieve that error message
function findUniq(arr) {
return undefined
}
Your function returned undefined when the expected answer was 1.
This comment has been hidden.
That's because your code is not correct. Print the input. Not a kata issue.
This comment has been hidden.
This comment has been hidden.
Because it's too slow and it times out. Not a kata issue.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I'm pretty confident that one your test cases is broken.
I'm pretty confident they're fine:
See which sample test your code fails and fix your code.
This comment has been hidden.
i made a mathematical solution it's working but floating point ruin it is there any idea to handle this problem?
9.999997563348018e-08 should equal 1e-07
:'(Checked OP's profile page and it seems he solved kata, so marking question as resolved.
what if i didn't solve it yet?? after all i didn't get any other rival solution with mathematical usage
anyways i gonna share my logical solution here as spoiler... at least not considered wasting of overthoughts
@Garce well in general when people ask questions it's because they want to solve the kata, so since you solved it it usually means you resolved your own question.
Alternatively, if you wanted a detailed explanation of your error, then you should post your code with spoilers rather than just 1 result from tests, because otherwise how can people guess why your code isn't working?
Re-reading your question, if you are getting floating point errors on a kata which involves counting elements, then it probably means you are introducing unncessary arithmetic operations like
*
or/
so that would be my guess why you get these mistakes (the kata is testing for strict equality since your result must be an item in the actual list - your current approach finds a value that is very close to a another value that is in the list, but does not actually appear:9.999997563348018e-08
is indeed close to1e-07
numerically, but it appears 0 times in the input list so it is not the unique number)Even
+
or-
with floats could lead to those errors. So, not the best choice here.lol even that wasn't my code in my view solution, however i understand how the codewars site's concept works, thank you guys for explaination :D
This comment has been hidden.
Why can it not be displayed in 3 test? Instead of 10 it outputs 3, but everything else works fine.
This comment has been hidden.
You passed the kata and nobody can answer the question as it is (that is, with no code). Please read this, closing.
Intenté hacerlo de la manera más sencilla (como aparece en la primera solución) y no me aceptaba por exceso de tiempo, lo cual me parece raro que otros compañeros pudieron hacerlo de esa manera y yo no. Igual pude con el kata
This has been by far the easiest kata for me. The code almost wrote by itself and I passed the tests at first try.
And that very very big array one? Looks like you did something like that before with performance optimization...
Helpful to find out how it's work
This comment has been hidden.
What will your solution return for
[1,0,0]
? What will it return for[0,1,0]
?This comment has been hidden.
tally
requires v2.7 of Ruby at least. I've enabled 3.0, try again, your code as it is, won't work.Awesome kata! Obviously, it need to add a test case having length equal 3.
This comment has been hidden.
C translation never tests for unique value in first position, therefore many invalid solutions are accepted.
Yeah, noticed that. I intentionally wrote code that shouldn't have passed to check if I haven't missed anything, and it passed on the first try :\
Added some fixed tests and two special random tests to C and C++.
This comment has been hidden.
If you don't know if there's something wrong with the kata or with your solution, it's not a kata issue ( yet ). Closing.
Please follow the troubleshooting guide to report issues. I can't even tell which language you're solving in.
This comment has been hidden.
That's a problem with your code, and not a kata issue. Use
Question
label next time and mark your post as having spoiler content too. Print the input, see which one makes your code fail.This comment has been hidden.
That's a problem with your code, not a kata issue. Use a faster algorithm.
I had the same problem in the big number test, the rest were passed with no problems.
Same, and i have absolutly no idea how to optimize code performance. I only know tuples are faster than lists and thats it lol
This comment has been hidden.
No, your code is returning an array (and it's not doing what the kata asks either), that's wrong. Not a kata issue.
This comment has been hidden.
i only have 3 lines of code, but it keeps failing at that one big number test due to time out, it did the same when i tried it with one line. (i thought something more compact might help but it didnt) :( idk why this keeps happening???
Hi; the number of lines of code doesn't say anything about the overall time complexity of an algorithm.
For example, if I write
for i in range(10**9971599599): print i
, it will also time out even though it is "only 2 lines".In your case, your algorithm is O(n^2) complexity; and the big test you are failing has approximately
8,000,000
elements in it - therefore you algorithm is performing approximately8000000 ** 2
operations which is very large! You need to find a faster approach (i.e. one that doesn't need to perform so many operations).ah thank you, such a simple sounding kata, yet its taking me so long haha
This comment has been hidden.
Did you read failure messages? Do you understand them?
Your solution is not entirely correct (but close). Errors in your solution are not a kata issue.
This comment has been hidden.
Your solution fails following test case:
assert.strictEqual(findUniq([ 0, 1, 1, 1, 1, 1, 1, 1]), 0);
Thank you very much
I ran the tests and all of them passed but when I pressed attempt it gave me this error Execution Timed Out (12000 ms) whats the problem ? anyone ?
Your code is too slow. Find a faster algorithm.
This comment has been hidden.
When I attempt my Code I get the error:
'The first item 1 should equal 0'
I am asking if there is a way to see the input array, so I can test my code. Since everything else is ok and there are no other errors thrown.
https://docs.codewars.com/training/troubleshooting/#print-input
I do not have the same output as shown in the images.
I only see the error message.
Did you add
print(arr)
to your code?Ok sorry, I am just not able to read the docs properly...
I also have the same problem, did you solve it?
This comment has been hidden.
My last solution is in fact wrong for all arrays with an odd number of items when the last item is the different one. ^^'
This comment has been hidden.
I completed this problem in 2 lines but yet I get a fail because of timeout
Execution Timed Out (12000 ms)
Your solution being too slow is not a kata issue.
You need more lines.
thank you. I re read the error and it said that
This comment has been hidden.
Your approach is too inefficient to pass this kata. The problem of the method you are using (in both codes you are providing; they are actually equivalent) is that for each item in the list, you need to make a complete loop over the list to get the value you are interested in. If the array has 10 elements, your code will have to make 10 * 10 operations. If the array has 1000 elements, then it will be 1000 * 1000, etc. It's easy to understand that this approach is extremely slow with large arrays. It's hard to give you a concrete answer without giving away the solution. Actually it is possible to solve this kata with only 2 * n operations (with n being the length of the array). Try to think how you could adapt your second code to do it. If you don't manage it, try to make some research with good keywords. In Python, there are some data structures available that almost do all the job for you.
Some more words: it's probably not useful to spend a lot of time on some katas, if your approach times out. That probably means you are missing some knowledge. A good advice I could give you is: try to practice on very easy katas (8/7 kyus). You will probably be able to solve a good amount of them without too much efforts. Once you solved a kata, you should have a look over the solutions that have been published by other users. By doing this, you will learn a lot of good tips and approaches you had no idea about, and you will see that you will also be able to solve many katas like this one easily. That's what I did, I learnt great part of what I know about programming by doing this.
Thank you very much for your advice. They are very, very helpful. I will work on the easy questions first - once again thank you so much.
i'm using php .. have solved this kata different ways, everytime i try to attempt my solution, i fail the advanced test due to 500ms excution time limit to pass .. have optimized my code narrowed it down just to 2 simple lines, still the same result .. any ideas?
Your solution being too slow is not a kata issue.
I just solved the kata in PHP and had no issues related to timeout. I think you need more than two lines.
ya i know .. being too slow is a php thing :D even it's just a few mseconds above the 500 mark .. to be honest modern computing became so powerful to even consider excution time.. have done it with just a couple of lines, sadly not fast enough .. i guess i'll keep trying .. thanx ..
I had the same issue in PHP. In the end, I copied the best solutions offered here and they all fail the speed test. :)))))
Someone added a performance test at some point, older solutions didn't have to pass that test. It's still possible to pass in PHP, you just need a faster solution.
This comment has been hidden.
Please use a spoiler flag when you give hints to a solution (I've just put the flag on for you).
Your approach is very expensive. Consider carefully all the data of the problem, you are doing much more work than actually needed.
Thanks, it was the first time I posted a question, so I didn't know about the spoiler flag. I'll keep it in mind.
This comment has been hidden.
This solution is invalidated.
I feel it would be better as a 7 kyu rating.
Rank won't be changed anytime soon.
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
Ah thank you
By 'The first item' test, I assume you mean, cases where the test is like this
arr = [1, 2, 2, 2]
. It's not passing because you initially assign 0 to n (totally fine), but then later, you're reassigning n to be arr[1], since arr[1] is not in non_unique at this point (at this point, non_unique only has arr[0]). Since you are adding items to an array and comparing, try adding the 2nd item into your non_unique array, then go from there. If you do add the 2nd item though, be careful to start your loop at the 3rd index of the array, and then compare. Not sure if that's giving away too much.This comment has been hidden.
This comment has been hidden.
Mind your language.
sure
Lua translation!
D sample tests:
Fixed
D translation
Two tests are failing "expected 9.999997563348018e-8 to equal 1e-7" and "expected 0.5699486632522621 to equal 0.5699486632387658", how can I reach this level of precision with JavaScript?
Don't use math operations.
This comment has been hidden.
Your code is too slow.
Why the author create a list named 'arr'?
This is absolutely not an issue (= a bug in the kata).
arr
is a very common variable name for instances oflist
.it comes from the word array
Please, Anybody! Approve these Pending C and C++ Translations! The author is not active half a year!
Approved :)
Thanks a lot!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Your code returns strings instead of numbers. Not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting
A bit tricky but succeeded. The code is tighter than I anticipated.
This comment has been hidden.
Your problem occurs if the unique value is the last value in the array. If that happens, your function
getResult(occurrence, null)
is called. In that case, you are creating a Listsurvivor
and populating it with every key that occurs in in your keylist1
time, which will be all of them. Keys naturally appear in HashMaps only once. The test that you are failing, the array is[8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 7.0]
. Right before you return your value,survivor
is[8.0, 7.0]
. I think what you wanted to do was get the key inoccurrence
where the value is1
, however, you never actually update any key inoccurrence
to have a value of more than1
at any time.Yeah, in the end I changed something, considering the case of the test 2. Now it went completed, it may be not the cleanest one, but for a first try on performance I don't mind the result
This comment has been hidden.
Please, don't spoil the kata. Mark your post as having spoiler content next time.
This comment has been hidden.
This comment has been hidden.
Don't post solutions in Discourse, solutions must be submitted, then they appear on the Solutions page, and that's their place. Once you solved a kata, you have access to all the other submitted solutions, it's a good idea to have a look at them to learn new things.
Also, whenever you post code, please use markdown tags to format it, otherwise it's unusable. See there: https://docs.codewars.com/training/troubleshooting/#post-discourse
Works fine in test cases but when i attempt i always get "Execution Timed Out" message
Your code not working is not an issue: https://docs.codewars.com/training/troubleshooting/
It is working tho in test but when I try to submit the error "Maxinum Call stack exeeced "
This comment has been hidden.
i am failing the very long arrays WHAT
This comment has been hidden.
I'm trying to debug my code but can't see what the failed test was? It just says what the test is called and that it failed. Am I missing how to check the test?
This comment has been hidden.
Hi. This is not an issue (= a bug in the kata). Please refer to the documentation: https://docs.codewars.com/training/troubleshooting/
arr.remove()
can be an expensive operation. The lists in the tests can have several million items in them.Edit: I ran your solution locally, for a list with 8.4 million items, it took about 9 minutes to complete.
you had me at
galring
what about complexity evaluation?
This comment has been hidden.
Your code not working is not an issue (= bug in the kata). This may help: https://docs.codewars.com/training/troubleshooting/
Sorry but that doesn't help. The code is in fact working in the console and should logically not return once too often. It works properly outside of this Kata. Also nothing in the description says why my approach would not be allowed. Or maybe I'm missing out on something then I'd appreciate a heads up.
Your code is just incorrect. All valid approaches to a problem are allowed: giving some specifications, you receive an input and must return a determined output, the kata just checks the result matches, it doesn't validate an approach.
It doesn't validate the approach. But in my other example (post above) it even returns NaN which makes no sense regarding that wherever else I execute the same exact code, it actually returns a valid number.
I have solved quite a few Katas here before and this is the first time that a simple thing like converting a '1' to a number leads to NaN lol. But downvoting my comments instead of actually offering some advice about it is easier I guess.
I gave you above a link to help you debug your solution. Did you read it? Did you take some time to see what your code is actually doing and returning when it fails? I guess you didn't, or you would see that for the first example your code returns
1
, which is incorrect. Don't you see it? Converting1
to a number doesn't giveNaN
. You post a buggy code and hopes someone gives his time and energy to save yours and corrects it, without following help you're provided. What do you hope?This comment has been hidden.
Sorry, I admit it's my fault. It doesn't fail when trying to convert '1' to a number. That was for the first example in the test and it works properly. Instead it fails on the next example trying to convert [0,1] to a number. I just realized that.
Print
unique
to the console.Yes just did, thanks. Was focusing on the wrong example, making false assumption too quickly.
Okay just found the correct solution, my first attempt was really a fail. Sorry for wasting your time, should had taken a closer look before spamming here.
This comment has been hidden.
Your code passes the
Sample Tests
, but not one of theATTEMPT
tests. Read the error to know what to fix.This comment has been hidden.
Your code is too slow for those tests, it'll work with small arrays but it'll fail when the size grows. Mark your post as having spoiler content next time.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting that's a problem with your code, it's not fast enough. Not a kata issue.
This comment has been hidden.
This comment has been hidden.
You could use
count()
if you do it the smart way. Using some other thing. Other solutions don't use count at all.thanks guys, I solved it but without an elegant solution xD. Happy coding y'all, appreciate it
it is showing expected 0 t0 1 error
this means your code returned 0 when the answer is 1
This comment has been hidden.
A problem with your code is not a kata issue:
Don't use nested loops.
This comment has been hidden.
There are solutions in Python that use list comprehension methods without timing out.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This kata needs to be fixed for Swift, using Double instead of Int in declarations, otherwise is impossible to pass the test cases.
Please try again: back up your code, reset your trainer, and run tests once more.
awesome, works perfectly now. no need to change Int for Double :)
same with me, i'm using javascript i got an error until i parse the value to Number why though???
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Normally the feedback for solutions should be in Solutions, not in Discourse. Link to your solution: https://www.codewars.com/kata/reviews/585d85846aa5e61d580004cd/groups/62012005be91d40001ccc863
{1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 1, 1}
you should add this to test code. Because some of "right" solutions returns wrong answers.
No:
ok, I misunderstood. Then we can do another kata with my variant :D
This comment has been hidden.
This comment has been hidden.
Please mark your post as having spoiler content next time. Print the input, analyze why your code fails with that.
When testing everything works fine but when I attempt I have two mistakes
That's a problem with your code, not a kata issue, read this: https://docs.codewars.com/training/troubleshooting
post your code, the test cases are limited
COBOL translation.
approved
This comment has been hidden.
Your solution is too inefficient. Not an issue.
Can you explain please, why is my solution consider "inefficient"?
if the list you were checking for was really big and had the unique element in the last, you would be iterating through all elements in the list to reach the answer. this is one way to write the function, but not a very efficient way, as it wastes a lot of time on unnecessary checks which can be cut down
You must use a spoiler flag when you post code somewhere (I put the flag for you this time).
Each time you call the
count
method, it needs to parse the whole list to count the number of elements matching its argument. You do that for every element, that's an aweful thing in terms of efficiency. If your list has 10 elements, your code needs to perform 100 operations (10 * 10). If your list has 1000 elements, 1,000,000 (1000 * 1000). And so on. It's easy to understand that with a large list that becomes incredibely huge. That's why it is said to be inefficient. You need to find a better approach.expected:<7.0> but was:<8.0> what this mean?
Your code produced the number
8.0
, but the correct number would have been7.0
.This comment has been hidden.
What happens when the unique value is at the first position as the test says? Your code is returning the other value. You can print the input and debug your code easier.
This comment has been hidden.
Print the length of the input at the start of your function. Read the last value before the crash.
Also: read the error message: "out of memory".
Then reconsider your life choices. :D recursion is a great tool ( I like it a lot! and I commend you for trying ), but in some languages it's not for every situation.
Thank You!
Generally no need to mark posts without code as spoilers BTW.
Spoilering a post means nobody gets notifications for it, and people who haven't solved in their selected language can't read it. If you don't have to spoiler, don't.
Hi, I keep getting an error when validating my solution, I tried 2 different solutions and still gets stuck.
Please follow the Troubleshooting Guide.
You're telling us nothing. Help us help you!
When testing out the code all goes well, but when trying to attempt timeout exceds and I even used 2 solutions.
The first one could've taken more time since it requires a validation of the whole array while the 2nd one doesn't and returns when finds x condition happen.
Please follow the Troubleshooting Guide.
This comment has been hidden.
Haskell tests are pathetic. There are definitely no "very huge arrays" - max length is, ehm,
21
and the reference solution isO(n log n)
, and probably a slow one for that complexity.See below how long this has been going on.
Haskell tests ( and example solution ) have been updated.
There are now huge tests. Also, tests with infinite lists. Lots of casualties, of course.
Why are there test cases with more than two different numbers? Doesn't the description say they will all be equal except one? Java version test: input:4.0 4.0 4.0 3.0 4.0 4.0 4.0 4.0 5.0 5.0 5.0 5.0 4.0 5.0 5.0 5.0 6.0 6.0 6.0 6.0 6.0 5.0 6.0 6.0 7.0 7.0 7.0 7.0 7.0 7.0 6.0 7.0 8.0 8.0 8.0 8.0 8.0 8.0 8.0 7.0 3.0 3.0 2.0 3.0 3.0 3.0 3.0 3.0 expected:<2.0> but was:<3.0>
There aren't. Those are the input of several tests together.
Python version says the unique number is an integer in the code scaffolding, despite unique float numbers being part of the tests and the problem description also making no assumption about the type of the unique number.
Changed to number.
This comment has been hidden.
Not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting/
What about random test? Is it cranky? I still have more than 2 numbers in array like this: Log 8348 8348 8348 3600 8348 8348 8348 8348 9942 377 9942 Test Failed Expected: 377 .... wtf?
Which language are you using? There are many.
c#
Кто подскажет? Решение мое удовлетворяет всем возможным массивам, в том числе и тем, которые выдаются в тестах. Однако, выдает следующее - 'expected undefined to equal 1'! Какое условие я упускаю? Спасибо.
Hi,
I passed all tests but i'm getting this error while submitting my solution in Javascript. Float number expected 0.0010001 to equal 1e-7
Am i missing something or is there an issue with this kata ? (I'm new )
Over 22 thousand submitted solutions - it's unikely you found a new issue. You are very probably just returning the wrong answer.
Try printing your inputs. If that doesn't help, go read the Troubleshooting Guide in Docs.
Hi, i just overcome the same error (using JS). This error occurs when you are manipulating with numbers e.g. sum or multiply. JS engines (and probably most of other languages) struggle with precision while calculating float numbers. The main idea is that in JS 0.1 * 0.2 != 0.02
Hi, How i'm supposed know the error if I dont know the arguements for the function? xd
This is shown: expected 0.8331198463974994 to equal 0.5743505469027179
https://docs.codewars.com/training/troubleshooting/#print-input
OK, thanks! Helped but not solved yet. Now I know my code is working for all cases except random tests with float numbers <0. Printed arr[0] and arr[1], just to check the number (chances of first two to be different is very low in those cases) and the return. Those two cases the test failed, but in the second one the number was not printed and the console displayed that menssage, as you can see:
0.33356082087588446 | 0.33356082087588446
0.32604033187919046
0.878128558117558 | 0.878128558117558 expected 0.878128558117558 to equal 0.9845884492667079
Wow, now I got it. My solution was right in the paper, but for some reason that I dont know, float numbers was causing numbers, so I avoided using them for comparisons. Thanks anyways.
Wish I was at the level where I can solve this as much as logically it sounds easy, I have no skill after copy-pasting codes to build my portfolio full of senior-level projects. I guess I gotta find a job and learn this as a hobby to become a better developer or plainly kill myself.
Don't kill yourself. I feel the same. Coding is hard. Just take it slowly and you'll see improvements. It doesn't need to become your life. Take it one step at a time
Coding is a good job if you love it or just got interested in it. If you see no meaning in it, find another job, but if you see some, learn it step-by-step. It's like any other job will start rewarding you, but unlike the others, it'll do it exponentially. Like "the more you give, the more you get**2"
This comment has been hidden.
This is not the place to post solutions. People who have not solved can see this solution when you post it like this. If you look at the top, you might notice a "Solutions" tab, which is where all solutions are automatically posted, so you can find yours there.
In the future, if you do need to post code in the discourse (for example when asking a question, or finding an issue with the kata itself), then please at least use the spoiler flag.
FSM!!!
This comment has been hidden.
Not able to submit, time out.
Please read this paragraph: https://docs.codewars.com/training/troubleshooting#kata-bug Then please read this paragraph: https://docs.codewars.com/training/troubleshooting#timeout
Resolving as not a kata issue. It's most probably a problem with your solution.
This comment has been hidden.
Yes, it can. Your solution has complexity of O(n^2), and is more or less equivalent to a loop nested in a loop because
count
goes through whole array every time.The kata can be solved in O(n), since it's enough to iterate over the array only once to solve it.
This comment has been hidden.
Please mark your posts as having spoiler content next time.
This comment has been hidden.
A problem with your code is not a kata issue:
Please read this: https://docs.codewars.com/training/troubleshooting
I am sure someone mentioned this but in Haskell if you write getUnique :: [Int] -> Int and than want to test it with [0, 0, 0.55, 0, 0] you do not get 0.55 but an error. The fix is simple just do getUnique :: Num a => [a] -> a This is Haskell 101 (perhaps 102) You should really change this.
hi! IMHO i make a realy fast solution with O(n/3)! 100000000 elements complite on 165 millis ))) May you see my work? One important note - i used Double.compare coz... we work with double values ))) If array contained integer values speed disastrously grow
the algorithm i wrote takes O(n) at its worst and O(1) (constant time) at its best.
hi! o(1) in best its obviously, if the element is the first ) I talk about worst of coz.
This comment has been hidden.
With over 22k python completions, I would wager the problem is on your end, so try again :)
This comment has been hidden.
Not a kata issue, your code is wrong, read this: https://docs.codewars.com/training/troubleshooting#print-input
Time exceeded for code atempt..
bug doesn't allow code to execute within timeframe. works for test but times out at attempt.
Not a kata issue, your code is too slow.
Your solution is timing out because it's not efficient enough. Think about what you can do so you don't necessarily have to go through every single number each time!
Hi, my solution passes all the tests except one: the first item. Here, it says that "1 should equal 0". I find this description very vague. First item of what, the input array? I would like some feedback, please. PS: I wrote the solution in Python.
Yes, what other interpretation would you think is possible? Print the input and it will be evident.
Thanks for the suggestion.
This comment has been hidden.
Please, C and C++ Translations!
This comment has been hidden.
That's not the test your code fails, print the input with
print_r($a);
, read this: https://docs.codewars.com/training/troubleshooting and also the PHP Docs on what you use in your code, the return type specially.This kata is quite similar to another kata.
This comment has been hidden.
https://www.codewars.com/kata/552c028c030765286c00007d
It's similar, yes, but there are different solutions for both, so, not a duplicate and not an issue.
"The first item: None should equal 0" what does this error mean?
That your code returned None instead of 0.
XCTAssertEqual failed: ("8") is not equal to ("7") - XCTAssertEqual failed: ("3") is not equal to ("2") - XCTAssertEqual failed: ("2") is not equal to ("1") -
This comment has been hidden.
This comment has been hidden.
Hi! Issues with swift, first one, the default func declaration has [Int] as parameter and [Int] as return type, when in the test cases Doubles are passed in the arrays, please change default function declaration for swift to func findUniq(_ arr: [Double]) -> Double
Second, I hace three test cases where my func does not work, and I am not sure why
I agree with changing the default parameter/return type declarations, as they're incompatible with the test cases. It's trivial to fix, but we shouldn't have to fix it. I haven't had any issues with the test cases, though.
This comment has been hidden.
yes, you can optimize this. which step do you think it is that's taking so long?
how can i make my code more efficent ?
I would define efficiency as having your code only do what is necessary to solve the problem, and no more. In this problem, for instance, it would be efficient for the search for a different number to end as soon as the different number is found.
refreshing kata!
I can't figure out the "random test" there are 6 or 7 different numbers and it expects a certain number but its random and usually there are multiple of that number. Is this supposed to be part of the test? The instructions don't indicate this will happen.
The C# random tests produce arrays with one value repeated over and over, and second value that shows up only once. Return the unique value.
Hm that's not what's happening when I log out the array its passing. Here's what the log says:
Log 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 1132 3583 2623 2623 2623 2623 2623 2623 5108 4943 4943 3899 2671 2671 2671 2671 2671 2671 2671 2671 2671 2671 2671 2671 2671 2671 1246 2876 2149 2149 9353 9353 9353 9353 7108 8785 8540 8540 8540 8540 8540 8540 8540 8540 8540 8540 8540 8540 3662 3662 3662 3662 3662 3662 3662 3662 3662 3662 3662 3662 1189 3379 1878 1878 1878 1878 1878 1878 1878 1878 4946 7805 4946 4946
Test Failed Expected: 7805 But was: 4946
That looks like you're printing the input of several tests together, try printing a carriage return after each test.
attempt doesn't work, it gives a timeout error
Your code is too slow.
buddy...I am using just 1 line of code to solve the problem...
That doesn't mean that is the fastest way of doing it. The post below yours also had a one line solution and the algorithm is too slow too. Check your code isn't O(n2)
buddy.. how about you learn some manners
This comment has been hidden.
This comment has been hidden.
No, your code works with the sample arrays, but it fails with the huge ones because it's too slow.
Thanks, no idea, i'm learning from 0, this is my first lenguage.
This comment has been hidden.
A problem with your code is not a kata issue. That will return always the first number.
In Javascript ....expected 1 to equal 0 ????
That doesn't mean
1 == 0
. It means the test expected0
but you returned1
. The wording is confusing, if taken literally.This comment has been hidden.
This comment has been hidden.
Don't post solutions in Discourse, please.
1 test in JavaScript is broken. "1 should equal 0"
No test is broken, they're fine. Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
I have the same problem. It's not broken although I can't seem to figure out how to fix it (I'm writing it in Python).
Can someone explain what the test condition here is? My code seems to fail only in this The first item 1 should equal 0
The unique number is the first item (like the test says). Print the input.
Im pretty sure my code is correct and this Kata is broken... Or am i just stupid?
Expected 3, Instead got 4 Expected 4, Instead got 5 ... ... Expected 1099511627776, Instead got 1125899906842624
Hard to tell as you don't state which language you're using. But for something like Javascript the kata has already been completed by over 18k persons.
there are over 18 thousand solves in javascript (your language), so yeah. the kata is not broken.
I have figured out the way to do it with high performance and i've passed all tests except for the float ones. For some reason the values in the array that i get dont match with the at all, i have tried to debug it by adding some prints to see if the array values were messed up but they look just like mine result gets.
I could pass the tests by comparing each element in the array, it wasnt a time problem because the float arrays had only 1000 elements, but i still think there was a problem with the float tests, since every other test with integers were successful.
The Python tests are ok. Your code has problems with float numbers because you're using floating point arithmetic.
This comment has been hidden.
This is the same problem as https://www.codewars.com/kata/57f609022f4d534f05000024
Hello guys, when I run my error code "Timeout 500ms". My code is only 4 lines long and has nothing that requires a lot of processing. What can I do?
This means that your algorithm is not productive for large values.Optimize with a faster algorithm
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Print the input: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-input you'll see why your code fails.
Why your else returns that?
This comment has been hidden.
I'll give you two input values so you can write your own tests and debug your code, as it is, it'll fail one of them:
Thanks. I noticed what is the problem with my codes. I might need to try in another way. Thanks for you advice.
This comment has been hidden.
You should open an issue here, because this is not a problem in the kata itself (and can't be addressed), the only available version in the site is 2.5.0
This comment has been hidden.
What's the problem?
In Javascript, the test 10 says expected 0 instead got 1. In the array there is one unique number and my code is displaying it correctly. I console log the array to check and should be correct
If you're not sure what happened then you don't know that there's an issue with the kata. This should be a question rather than an issue. Judging by wording of this report and the number of javascript completions, this isn't an issue, whatever it is. Please prove me wrong.
This comment has been hidden.
There is no problem with the tests. Make sure you're reading the logs the right way, they appear above the test result. Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Hi, swift version not compiling because method "findUniq" returns Int, but some of the test cases have Double numbers. Does anyone have any suggestions here? Thanks
I saw this too. The creator will have to fix their test cases.
i am coding in python, and all tests passed except the 6th, 7th, and 8th under basic tests. 6th (Value=6) says "8 should equal 7" 7th (Value=7) says "3 should equal 2" 8th (Value=2) says "2 should equal 1"
I passed all other tests, even the crazy big ones. I find it hard to believe i passed 52, but still failed 3. I believe my code is right. Anyone else have this problem?
the same problem
This comment has been hidden.
This comment has been hidden.
All tests passed except one , which says 1 should equal 0 , its the tenth test... Im pretty sure my code is good. What could I do?
You could tell what language you have the problem with.
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution That's a problem with your code, it's not ok. Check when and how creating a kata issue in the aforementioned wiki.
This comment has been hidden.