0

How to find a second largest number in a an array without sorting and swiping?plz Help, ex array {1,5,10,3,8,4,9,2}

8th Jul 2022, 3:07 PM
GNANAMURTHY
3 Respostas
+ 3
1. Create variables for max and 2nd max number 2. Initialize them with something not bigger than the max number 2. Loop through array items 3. Everytime you find a bigger number, update the 2 variables accordingly 4. Take care of the edge case where the initial variables values is the max value
8th Jul 2022, 3:53 PM
Emerson Prado
Emerson Prado - avatar
+ 2
GNANAMURTHY , as long as we have not seen your attempt, we can not help you in detail but give you some hints: (1) if you are allowed to use 2 array functions which returns the max and min element, we can do it like this (using an iteration): -> use a function that returns the max number from the array and store it in max_ -> create a variable scnd_largest and intialize it with min element -> use a loop and iterate through the array getting one number at a time that is stored in variable num -> conditional: if num > scnd_largest and num < max_, then num will be assigned to scnd_largest -> if loop is done, scnd_largest holds the second largest number of the array (2) in case a function that returns the max or min number is not allowed, we have to run a separate iteration cycle and then continue like in sample (1).
8th Jul 2022, 6:08 PM
Lothar
Lothar - avatar
+ 1
Can you share your attempt? edit: something like you need a loop, and traverse each element and check current element is greater than firstMax and less than secondmax element then change.. if it is gretaer to all thrn take as firstmax, ptiovius first max as second max. before this, initially assign from first 2 element max one as first max, next is second max.. continue this till array length. if array length is less than 2 then return -1 hope it helps..
8th Jul 2022, 5:12 PM
Jayakrishna 🇮🇳