Skip to content

Commit 427022b

Browse files
authored
Add Linear Search in Scala #5144 (#5204)
1 parent fe85184 commit 427022b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

archive/s/scala/LinearSearch.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
object LinearSearch {
2+
def main(args: Array[String]): Unit = {
3+
if (args.length != 2) {
4+
println("Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")")
5+
return
6+
}
7+
8+
val list = args(0);
9+
val key = args(1);
10+
11+
try {
12+
val arr = list.split(",").map(_.trim.toInt)
13+
val target = key.trim.toInt
14+
15+
var flag = 0
16+
var pos = -1
17+
for (i <- arr.indices) {
18+
if (arr(i) == target) {
19+
flag = 1
20+
pos = i
21+
println("true")
22+
return
23+
}
24+
}
25+
if (flag == 0) {
26+
println("false")
27+
}
28+
} catch {
29+
case _: NumberFormatException =>
30+
println("Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")")
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)