Last active
April 27, 2021 04:16
-
-
Save osahondev/00c8851811fbf4ba5b47b2c8d1a94890 to your computer and use it in GitHub Desktop.
Function to return the position of values in a sorted array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.