0

Can someone tell me what is the problem with this code?

I was writting the code to count the number of non repeated element in an array but it is giving "no output". Is the logic applied is wrong or there is any other problem. https://code.sololearn.com/cFd0zL8bllf4/?ref=app https://code.sololearn.com/cFd0zL8bllf4/?ref=app

22nd Sep 2019, 7:06 PM
Abhishek Dimri
Abhishek Dimri - avatar
3 Answers
+ 4
On line 11 you need to put ampersand operator '&' before a[i] to get values stored in your array. Also initialize your variables to 0 or some value at the time of declaration to avoid logical errors which you will face in your code. Lastly, you don't need nested loops for checking repetition of number. Your logic is somewhat right but fully correct. Let's say I have an array of four elements: 1 2 3 3 Two non-repeating numbers. As per your logic, //First iteration i = 0, j = 0 i != j (False) i = 0, j = 1 i != j (True) a[i] == a[j] (False) count2++; i = 0, j = 2 i != j (True) a[i] == a[j] (False) count2++; i = 0, j = 3 i != j (True) a[i] == a[j] (True) count1++; i = 0, j = 4 i != j (True) a[i] == a[j] (True) count1++; Till now we get correct answer but after 2nd iteration again same steps will be repeated and count2 will be incremented. So it will give incorrect output.
22nd Sep 2019, 7:41 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
[Continued...] So here's the code, dry run it yourself and see how its working for(i=0;i<n-1;i++) { if(a[i] == a[i+1]) count1++; else count2++; } Don't forget to initialize your variables.
22nd Sep 2019, 7:41 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
okay suppose the array is 3 1 2 3 then this programme will not work
23rd Sep 2019, 3:43 AM
Abhishek Dimri
Abhishek Dimri - avatar