Skip to content

Commit 79f8eb9

Browse files
authored
Merge pull request #54 from khanhhuynguyenvu/feature_search
Feature search
2 parents 46aa813 + 0bb2d6d commit 79f8eb9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/main/scala/Search/LinearSearch.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ object LinearSearch {
1111
* @return - index of the @elem or -1 if elem is not fount in the @arr
1212
*/
1313
def linearSearch(arr: List[Int], elem: Int): Int = {
14-
//the functional way, common in scala would be:
15-
//args.indexOf(target)
16-
for (i <- arr.indices if (arr(i) == elem)) {
17-
return i
14+
def iter(index: Int): Int = {
15+
if (index != arr.length) {
16+
if (arr(index) != elem) iter(index + 1)
17+
else index
18+
} else
19+
-1
1820
}
19-
-1
21+
22+
iter(0)
2023
}
2124

2225
}

0 commit comments

Comments
 (0)