You need to sign in to do that
Don't have an account?
fiona 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
Regards,
Fiona
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
All Answers
try below code. I don't think you will be need for map to achieve this. don't forget to mark it as best answer.
Thank you
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