unexpected value in array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 17

unexpected value in array

I have written the following code. Description of the question is given in the code, I am unable to find what is going wrong but it is saving 2 values at. same position. Please help me correct this code. Thanks https://code.sololearn.com/cM54OaWbm3uk/?ref=app

2nd Jun 2019, 1:37 PM
Night_fury~
Night_fury~ - avatar
6 Answers
+ 4
So a couple of things: 1. Whenever you are trying to swap values, you need to save the value that you are overwriting. For example: Given 1, 3, 4, 2 as input. When you get to position 2 that has a 3 in it, you replace the 4 in position 3 with 2. So now you have 1, 3, 2, 2. 2 is in pos 3, so 3 will be placed in pos 2: 1, 3, 2, 2 2 is in pos 4, so 4 will be placed in pos 2: 1, 4, 2, 2 2. Givin is spelled given
2nd Jun 2019, 4:02 PM
Zeke Williams
Zeke Williams - avatar
+ 17
thanks Zeke Williams and ~ swim ~ 🤓🤓
2nd Jun 2019, 5:16 PM
Night_fury~
Night_fury~ - avatar
+ 15
I will do it by both methods
3rd Jun 2019, 12:09 AM
Night_fury~
Night_fury~ - avatar
+ 14
yeah thanks for pointing that out ~ swim ~ I will correct it but even if we input 1,4,3,2 then also it is not giving correct output.
2nd Jun 2019, 2:21 PM
Night_fury~
Night_fury~ - avatar
+ 14
please check it for this (1,4,3,2) input. thanks
2nd Jun 2019, 2:32 PM
Night_fury~
Night_fury~ - avatar
+ 2
Night_fury~ you're welcome! And ~ swim ~ mentioned another solution; you could just create a separate output 'array' too
2nd Jun 2019, 7:35 PM
Zeke Williams
Zeke Williams - avatar