Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Created October 23, 2025 14:13
Show Gist options
  • Save kieranbarker/0a56fcdd0070eae2974fb9067fc786f6 to your computer and use it in GitHub Desktop.
Save kieranbarker/0a56fcdd0070eae2974fb9067fc786f6 to your computer and use it in GitHub Desktop.
public class LinearSearch {
public static int linearSearch(String[] haystack, String needle) {
for (int i = 0; i < haystack.length; i++) {
if (needle.equals(haystack[i])) {
return i;
}
}
return -1;
}
}
@accuser
Copy link

accuser commented Oct 23, 2025

public class LinearSearch {
	public static int linearSearch(String[] haystack, String needle) {
		int result = -1;
		
		for (int i = 0; i < haystack.length; i++) {
			if (needle.equals(haystack[i])) {
				result = i;
				break;
			}
		}
		
		return result;
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment