max function code example

Example 1: max value in array javascript

// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });

// For arrays with relatively few elements you can use apply: 
const max = Math.max.apply(null, arr);

// or spread operator:
const max = Math.max(...arr);

Example 2: python max()

i = max(2, 4, 6, 3)

print(i)
# Result will be 6 because it is the highest number

Example 3: python max with custom function

nums = [1,2,3,1,1,3,3,4,4,3,5] #list
counts = collections.Counter(nums) # create a dict of counts
#using counts.get() fun as custom function 
return max(counts.keys(), key=counts.get)

Example 4: max func in python

max([2, 1, 4, 3])
#Returns 4 as it's the largest integer in the list

Example 5: return max(max(a,b),max(c,d));

return max(max(a,b),max(c,d));