0
Counting odd repeating number of times
https://code.sololearn.com/ca11A5a0A9A1 Enter the array size: 8 Enter the numbers in the array: 5 7 8 8 5 8 7 7 the array is: 5 7 8 8 5 8 7 7 the number repeating odd number of times: 7 this was the output of this code.My question is here 8 is also repeating 3 times so why not return 8 instead of 7.
3 Réponses
+ 1
There you are returning 7,because there is odd number of 7s in your array. You start counting with 1st element, which is 7, if you make your array count 8 before 7, it will return 8 (if you keep it odd)
So, array size: 8
Numbers in the array: 5 8 7 7 5 7 8 8
This way, you count number of 5s in the array, which is even, so it goes to 8, number of 8s is odd, so 8 is returned
Btw, why you make that array of 100 elements, when you can allocate it dynamically for "n" elements?
+ 1
Because your array start from 7.
And you program start counting from 7.
So 7 was counted and returned even before counting of 8. Your program didn't even tried to count number 8.
+ 1
@Michal yes I didn't use dynamic array where I could have used..there is no reason for using an array of 100 elements...
Thank you both @Shail