Skip to content
Loading
  • +2
    Hello, Abdalla.
     
    Indeed, it does return an object, not the max value. 
     
    First off, I look to make sure we aren't in the degenerate situation of having no list or an empty list, and return null in that case.
     
    Then, I look at the list... Take the last object, and assume he has the maximum value. I keep track of a reference to the object in the list as an integer, using the fact that the first item in your list is List[0], followed by List[1], ... List[n-1].
     
    So I walk the list from the back to the front (the currentIndex is decremented) and keep track of the index of the item with the lowest value. Note that I don't have to store that value, I can compare the value in the current object against the value in the object we have currently marked as the max. Where I find a larger value, I revise the index to the current index.
     
    The way it's coded, it'll work properly for the edge case of having a list with just 1 item. 
     
    At the end, I print the maxIndex to show how you can get it if you need it, but return the object itself. 
    +2
  • Laxmidhar Sahoo
    0
    hi omran you are currect
     
    1. public static double CalculateMaxVal (List input)  
    2. {  
    3. double maxValue = double.MinValue;  
    4. foreach (var item in input)  
    5. {  
    6. if (item.SecondsDiff > maxValue)  
    7. {  
    8. if(item.SecondsDiff != null)  
    9. maxValue = item.SecondsDiff;  
    10. }  
    11. }  
    12. Console.WriteLine(maxValue);  
    13. return maxValue;  
    14. }  
     
    0
  • 0
    Laxmidhar Sahoo , 
    Thank you for your Code but i want to get the object back and not the maxvalue . the maxvalue is already holding by an Object and that want give back . hope so you have got me what i meant.  
    0
  • 0
    Michael Extwooh
    I thank you for your answer but could you explain to me what did you coded . its not clear for me .does give the object back ? 
    0