function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hariharan M 18Hariharan M 18 

create list on numbers

can anyone solve the below questions?
create list on numbers
-> 1.find biggest number in that list
-> 2.find sum of odd and even numbers in the list (seperately)
-> 3.find any negative numbers in the list
-> 4. count number of negative numbers in the list and print the count
-> 5.reverse a list and store in a new list, print the new list
Best Answer chosen by Hariharan M 18
PriyaPriya (Salesforce Developers) 
Hey Hariharan,

1. To find biggest number in that list
List<Decimal> samplevalues = new List<Decimal>();
Decimal maxvalue = samplevalues[0];
        For (integer i =0;i<samplevalues.size();i++)
            if( samplevalues[i] > maxvalue)
                maxvalue = samplevalues[i];             
system.debug('the max value is'+maxvalue);
2. sample logic to caluclate the sum of odd number in collection 
List<Integer> intList = new List<Integer>() ;
Integer sum =0 ; 
for(Integer i = 0 ; i<intList.size() ;i++){
   if (Math.mod(i, 2) != 0) {
   sum= sum+intList[i];

3. to count the number of negative number, take the reference from here :-

5. To reverse the list and store in new list :-
List<Object> someList = new List<Object>{1,2,3,4,5};
List<Object> reversed = new List<Object>();

for(Integer i = someList.size() - 1; i >= 0; i--){

Kindly mark it as the best answer if it works for you.


Thanks & Regards,

Priya Ranjan