FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript/search/linear_search.ts at master · TheAlgorithms/TypeScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
TypeScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
578
Star
2.9k
Code
Issues
24
Pull requests
50
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeScript
/
search
/
linear_search.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
18 lines (18 loc) · 766 Bytes
Breadcrumbs
TypeScript
/
search
/
linear_search.ts
Copy path
File metadata and controls
18 lines (18 loc) · 766 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
*
@function
linearSearch
*
@description
linear search is the simplest search possible in a array
* it has a linear cost, if the value is present in the array, then the index of the first occurence will be returned
* if it's not present, the return it will be -1
*
@param
{
number[]
} array - list of numbers
*
@param
{
number
} target - target number to search for
*
@return
{
number
} - index of the target number in the list, or -1 if not found
*
@see
https://en.wikipedia.org/wiki/Linear_search\
*
@example
linearSearch([1,2,3,5], 3) => 2
*
@example
linearSearch([1,5,6], 2) => -1
*/
export
const
linearSearch
=
(
array
:
any
[
]
,
target
:
any
)
:
number
=>
{
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
if
(
array
[
i
]
===
target
)
return
i
}
return
-
1
}
Back
|
FazBrowse Home
|
New Git URL