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
fiona gentryfiona gentry 

How to find smallest number in array using Map

Dear folks,

How to find smallest number in array using Map

Example 1:

Input: nums = [2,5,6,9,10]

Explanation:
The smallest number in nums is 2.


Here is code tried using Maps but no success
public class GCDArray {
    public static List<String> greatestdiv(String input) {
        system.debug(input);
        List<String> inputList = input.split('');
        system.debug(inputList);
        Map<String,Integer> counts = new Map<String,Integer>();
        system.debug(counts);
        for(String s1:inputList) {
            counts.put(s1,0);
            system.debug(counts);
        }
        for(String s1:inputList) {
            counts.put(s1,counts.get(s1)+1);
        }
        system.debug(counts);
        String maxKey = counts.isEmpty()?null:new List<String>(counts.keyset())[0];
        system.debug(maxKey);
        for(String s1:counts.keySet()) {
            maxKey = counts.get(s1)>counts.get(maxKey)?s1:maxKey;
        }
        system.debug('The smallest number in array is'+ maxKey);
        system.debug(counts.get(maxKey));
        
        List<String> resultList = maxKey.split('');
        for( Integer S : counts.keySet() ){
            system.debug(S);
            resultList.add(S,counts.get(S));
            system.debug(resultList);
            
            
        }
        
        
        return resultList;      
    }
    
    
}

Regards,
Fiona
 
Best Answer chosen by fiona gentry
Jayeshkumar ParmarJayeshkumar Parmar
Hi fiona gentry,



Try Below Code Using Array,

Integer[] myList = new List<Integer> {5,4,1,2,10 };
myList.sort();
System.debug('Smallest Value : '+myList[0]);


OR  You can also Try this using map,


Map<String,Integer> mapData = new Map<String,Integer>{'a' => 10, 'b' => 8, 'c' => 4,'d' =>10,'e' => 6};

String minKey = mapData.isEmpty()?null:new List<String>(mapData.keyset())[0];
for(String s1:mapData.keySet()) {
  minKey = mapData.get(s1) < mapData.get(minKey)?s1:minKey;
}
System.debug('Smallest Value '+mapData.get(minKey));
If this helps,please mark it as best answer

All Answers

ravi soniravi soni
hi fiona,
try below code. I don't think you will be need for map to achieve this.
list<integer> lstInteger = new list<integer>{20,2,5,1,0,6,8,26,9,10,3,11};
system.debug('lstInteger before : ' + lstInteger);
    lstInteger.sort();
system.debug('lstInteger after : ' + lstInteger);

system.debug('the smallest number is  : ' + lstInteger[0]);
system.debug('the smallest number is  : ' + lstInteger[(lstInteger.size()-1)]);
don't forget to mark it as best answer.
Thank you
 
Jayeshkumar ParmarJayeshkumar Parmar
Hi fiona gentry,



Try Below Code Using Array,

Integer[] myList = new List<Integer> {5,4,1,2,10 };
myList.sort();
System.debug('Smallest Value : '+myList[0]);


OR  You can also Try this using map,


Map<String,Integer> mapData = new Map<String,Integer>{'a' => 10, 'b' => 8, 'c' => 4,'d' =>10,'e' => 6};

String minKey = mapData.isEmpty()?null:new List<String>(mapData.keyset())[0];
for(String s1:mapData.keySet()) {
  minKey = mapData.get(s1) < mapData.get(minKey)?s1:minKey;
}
System.debug('Smallest Value '+mapData.get(minKey));
If this helps,please mark it as best answer
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

hi fiona,
you can find minimum element from an array without map but if you want to use map then you have to specify about key value pair and on 

what basis you want to find whether on the basis of key or value , I am write code to find minimum element given below.

list<integer> arrayList = new list<integer>{20,2,5,1,0,6,8,26,9,10,3,11};

if(arrayList.size()>0){
integer min = arrayList[0];
for(integer i =1;i<arrayList.size();i++){
if(arrayList[i]<min){
min = arrayList[i];
}
}
}
system.debug("Minimum element of an array is ====> "+min);


If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi