Skip to content

Commit 92d4702

Browse files
authored
fix: handle null and empty array in LinearSearch (issue #7340) (#7347)
1 parent cc75b5e commit 92d4702

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/main/java/com/thealgorithms/searches/LinearSearch.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ public class LinearSearch implements SearchAlgorithm {
4141
*
4242
* @param array List to be searched
4343
* @param value Key being searched for
44-
* @return Location of the key
44+
* @return Location of the key, -1 if array is null or empty, or key not found
4545
*/
4646
@Override
4747
public <T extends Comparable<T>> int find(T[] array, T value) {
48+
if (array == null || array.length == 0) {
49+
return -1;
50+
}
4851
for (int i = 0; i < array.length; i++) {
4952
if (array[i].compareTo(value) == 0) {
5053
return i;

0 commit comments

Comments
 (0)