What is the code for finding the number of times the minimum value appears in an array x | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the code for finding the number of times the minimum value appears in an array x

13th Mar 2021, 1:03 AM
Jayasudha selvan
Jayasudha selvan - avatar
6 Answers
+ 3
DHANANJAY PATEL arf, yes: but you need to not forgot to reset counter to 0/1 when a new minimum is encountered ^^
13th Mar 2021, 3:57 AM
visph
visph - avatar
+ 2
DHANANJAY PATEL run together? did you mean only one loop? yes, it could be done in only one loop, but you must count every items occurence... as you don't know what's minimum value... that's seems more reasonable to do this by two independant loops, at least for beginner (and also for memory footprint): iterating twice an array doesn't provide a big time difference, unless you're working with wide data set (and if so, memory footprint as well as data structure creation/population should at best produce quite equivalent efficiency ^^
13th Mar 2021, 3:33 AM
visph
visph - avatar
+ 2
visph Yes in single loop It's not complex logic Start with first value as minimum and set occurrence counter to 1 When ever you encounter new minimum reset counter to 1 and set new minimum If minimum is equal to current minimum increment minimum counter by 1 Please don't mind it two loops are not mandatory Thanks for explaining that it is not needed when you are processing lesser data. DHANANJAY
13th Mar 2021, 3:53 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
you must iterate once the array to find the mnimum value, then iterate another once again to count how many minimum is encountered ;)
13th Mar 2021, 1:09 AM
visph
visph - avatar
+ 1
According to me both can run together. While finding minimum value counter can be reset to one(1) each time minimum new minimum value encounter and if value is same as minimum value counter can be incremented by one(1). DHANANJAY
13th Mar 2021, 1:47 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
When processing array for minimum it needs to be reset to 1 as the new minima is found it is counted as 1(first) minima. DHANANJAY
13th Mar 2021, 6:56 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar