Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active April 27, 2021 04:16
Show Gist options
  • Select an option

  • Save osahondev/00c8851811fbf4ba5b47b2c8d1a94890 to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/00c8851811fbf4ba5b47b2c8d1a94890 to your computer and use it in GitHub Desktop.
Function to return the position of values in a sorted array.
const getPosition = ( nums, val ) => {
let idxs = [-1,-1];
let idx = 0;
let idx_val = 0;
if( !Array.isArray(nums) ) return idxs;
if( (nums == undefined) || (nums == null) ) return idxs;
if ( nums.length == 0 ) return idxs;
nums.sort( (a,b)=>a-b );
for(const num of nums){
if( num == val ){
idxs[idx] = idx_val;
idx++;
}
if( idx > 1 )
return idxs;
idx_val++;
}
return idxs;
}
@meekg33k
Copy link

Hi @eniaho, I have written a blog post here sharing my solution to this problem. Please read through and let me know what you think.

I hope this was a good learning experience for you. Thank you once again for participating once again and see you on Friday for Week 4 of #AlgorithmFridays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment