The \\ operator is the list difference, and the .. can be used on any enum type to build a list. This means this works for a large number of types, like:
exportconstminMinMax=(set:number[],):[number,number|undefined,number]=>{if(set.length<1){thrownewError('set must include at least one number')}constsorted=[...set].sort((a,b)=>a-b)constsmallest=sorted[0]constlargest=sorted[set.length-1]letminimumAbsent=smallest+1for(leti=1;i<sorted.length;i++){if(minimumAbsent!==sorted[i])breakminimumAbsent++}return[smallest,minimumAbsent>largest?undefined:minimumAbsent,largest,]}
Test
import{minMinMax}from'.'it('should throw an error if less than one element',()=>{expect(()=>minMinMax([])).toThrow()})it('should return smallest, minimumAbsent, largest from array of numbers',()=>{expect(minMinMax([-1,4,5,-23,24])).toStrictEqual([-23,-22,24])expect(minMinMax([1,3,-3,-2,8,-1])).toStrictEqual([-3,0,8])expect(minMinMax([2,-4,8,-5,9,7])).toStrictEqual([-5,-3,9])})it('should return undefined for minimumAbsent if not present between smallest and largest',()=>{expect(minMinMax([-3,-2,-1,0,1,2,3])).toStrictEqual([-3,undefined,3])})it('should support a single number',()=>{expect(minMinMax([1])).toStrictEqual([1,undefined,1])})it('should support two numbers',()=>{expect(minMinMax([1,2])).toStrictEqual([1,undefined,2])expect(minMinMax([1,3])).toStrictEqual([1,2,3])})
Hey, I'm Steven Mercatante and I build things for the web.
I'm a full stack software engineer with over 15 years of experience building websites, mobile apps, APIs, CMSs, and ETL pipelines.
functionminMinMax(arr){// Sorting `arr` lets us easily select the min and max valuesarr.sort((a,b)=>a-b)constmin=arr[0]constmax=arr[arr.length-1]// Starting at the `min` number, search between // `min` and `max` until you find a value that's not in `arr`letminimumAbsent=nullfor(leti=min;i<max;i++){if(!arr.includes(i)){minimumAbsent=ibreak}}return[min,minimumAbsent,max]}minMinMax([-1,4,5,-23,24]);//[-23, -22, 24]// minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]// minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3, 9]
Explanatory post. Thanks for sharing with US. I am lucky to read this content. Sure. But Array#sort is secondary here, it works with any array on the right hand side of an assignment thank you from walgreenslistens blog for walgreens survey
Haskell solution:
The
\\operator is the list difference, and the..can be used on any enum type to build a list. This means this works for a large number of types, like:I'm sure there's less time complexity one but this boi will get its job done
TypeScript with tests
github.com/kiliman/dev-to-daily-ch...
Test
Here is the PHP code snippets:
My solution in Swift, I check if the array is not empty and bigger than two elements :
Elm
Playground
Here.
Runnable example:
Here is a solution with LabVIEW. Not tried LabVIEW? The community edition beta is now open!🎉🎉
Explanatory post. Thanks for sharing with US. I am lucky to read this content. Sure. But Array#sort is secondary here, it works with any array on the right hand side of an assignment thank you from walgreenslistens blog for walgreens survey
Can you explain this part? I haven't seen sort used like this before.
Holy crap I had no idea you could do that in Ruby. Thank ya for the old post that explained it very clearly.
That is fascinating, I had no idea. I will keep that in mind.
Lucky to read this article, this is amazing, thanks for sharing!
coloring-pages.io