-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
33 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
(function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
|
||
/** | ||
* Searches for specific element in a given array | ||
* using the linear search algorithm | ||
* Time complexity: O(n) | ||
* | ||
* @param {Array} array Input array | ||
* @param {Number} key the number whose index is to be found | ||
* @returns {Number} the index of the first instance of number or else -1 if not found | ||
*/ | ||
/** | ||
* Searches for specific element in a given array | ||
* using the linear search algorithm | ||
* Time complexity: O(n) | ||
* | ||
* @param {Array} array Input array | ||
* @param {Number} key the number whose index is to be found | ||
* @returns {Number} the index of the first instance of number or else -1 if not found | ||
*/ | ||
|
||
const linearSearch = (array, key) => { | ||
for (let i = 0; i < array.length; i++) { | ||
if (array[i] == key) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
const linearSearch = (array, key) => { | ||
for (let i = 0; i < array.length; i += 1) { | ||
if (array[i] === key) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
|
||
exports.linearSearch = linearSearch; | ||
})(typeof window === 'undefined' ? module.exports : window); | ||
exports.linearSearch = linearSearch; | ||
})(typeof window === 'undefined' ? module.exports : window); |
This file contains 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