Dart Program to Implement Linear Search

Dart Program to Implement Linear Search

Learning how to search through data is one of the first skills every beginner programmer needs. Linear search is one of the simplest ways to find a specific item in a list, making it a perfect starting point for anyone new to coding. In Dart, a modern programming language used for mobile, web, and desktop applications, implementing a linear search is straightforward and gives a great introduction to loops, conditions, and functions.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Linear search is widely used when the dataset is small or unsorted. Even though more advanced searching algorithms exist, linear search is easy to understand and helps you build the foundation needed for more complex tasks. By the end of this article, you’ll be able to write multiple Dart programs to perform linear search in different ways, including using loops, recursion, and even using Dart’s built-in functions.

Program 1: Linear Search Using a For Loop

This program demonstrates the most basic way to perform a linear search. It checks each element in the list one by one until it finds the target value.

void main() {

  List<int> numbers = [3, 5, 7, 9, 11, 13];
  int target = 9;
  bool found = false;

  for (int i = 0; i < numbers.length; i++) {

    if (numbers[i] == target) {

      print("Element $target found at index $i");
      found = true;
      break;

    }

  }

  if (!found) {
    print("Element $target not found in the list");
  }

}

In this program, the for loop goes through every element of the list until it finds the one that matches the target value. If it finds the target, it prints the index and stops searching. This approach is very beginner-friendly because it shows exactly how the program checks each element one by one.

Program 2: Linear Search Using a While Loop

This example uses a while loop instead of a for loop, offering another way to achieve the same result. It gives beginners a chance to practice a different type of loop.

void main() {

  List<int> numbers = [10, 20, 30, 40, 50];
  int target = 30;
  int i = 0;
  bool found = false;

  while (i < numbers.length) {

    if (numbers[i] == target) {

      print("Element $target found at index $i");
      found = true;
      break;

    }

    i++;

  }

  if (!found) {
    print("Element $target not found in the list");
  }

}

Here, the while loop continues to check elements as long as the index i is less than the length of the list. The logic is very similar to the for loop example, but it demonstrates another looping technique. Beginners can see how control structures in Dart allow multiple ways to solve the same problem.

Program 3: Linear Search Using a Function

In this program, the linear search is implemented inside a reusable function. Functions make code cleaner and easier to manage, which is important as programs grow larger.

int linearSearch(List<int> numbers, int target) {

  for (int i = 0; i < numbers.length; i++) {

    if (numbers[i] == target) {
      return i;
    }

  }

  return -1; // returns -1 if not found

}

void main() {

  List<int> numbers = [1, 3, 5, 7, 9];
  int target = 7;
  int result = linearSearch(numbers, target);

  if (result != -1) {
    print("Element $target found at index $result");
  } else {
    print("Element $target not found in the list");
  }

}

By putting the search logic in a function, you can reuse it anywhere without rewriting the same code. This program also introduces the concept of returning values, which is a key programming skill. Beginners can easily see how functions help in organizing code.

Program 4: Linear Search Using Recursion

Recursion is another way to implement linear search. Instead of using loops, the function calls itself until it finds the target or reaches the end of the list.

int linearSearchRecursive(List<int> numbers, int target, int index) {

  if (index >= numbers.length) {
    return -1; // element not found
  }

  if (numbers[index] == target) {
    return index;
  }

  return linearSearchRecursive(numbers, target, index + 1);

}

void main() {

  List<int> numbers = [2, 4, 6, 8, 10];
  int target = 6;
  int result = linearSearchRecursive(numbers, target, 0);

  if (result != -1) {
    print("Element $target found at index $result");
  } else {
    print("Element $target not found in the list");
  }

}

Recursive linear search teaches beginners how a function can call itself to solve smaller parts of a problem. It’s useful in understanding recursion and thinking about problems in a different way compared to loops. It also shows how the same problem can be approached in multiple ways.

Program 5: Linear Search Using Dart’s Built-in indexOf Method

Dart has a built-in method called indexOf that makes linear search very simple. This example demonstrates a practical way to use Dart’s standard library for searching.

void main() {

  List<int> numbers = [5, 10, 15, 20, 25];
  int target = 15;

  int index = numbers.indexOf(target);

  if (index != -1) {
    print("Element $target found at index $index");
  } else {
    print("Element $target not found in the list");
  }

}

Using indexOf simplifies the code because you don’t have to write loops or recursion manually. It’s useful for beginners to see how built-in functions can save time and make programs cleaner while still performing the same task.

Frequently Asked Questions (FAQ)

Learning linear search can raise some common questions for beginners. Here are a few that might help.

Q1: What is linear search used for?
Linear search is used to find a specific element in a list or array, especially when the list is unsorted or small. It is simple and easy to implement.

Q2: Is linear search fast?
Linear search is not very fast for large lists because it checks each element one by one. For bigger datasets, algorithms like binary search are faster.

Q3: Can I use linear search with strings?
Yes, linear search works with strings, characters, or any other type of data in a list. The logic is the same.

Q4: Why should I learn recursion for linear search?
Recursion teaches you a different way of thinking about problems, helps in solving more complex problems, and strengthens your understanding of functions in Dart.

Q5: Can Dart built-in functions replace linear search?
Yes, methods like indexOf or contains can perform linear search behind the scenes, but knowing how to implement it manually is important for learning and understanding the basics.

Conclusion

Linear search is a foundational concept that every beginner programmer should know. By learning to implement it in Dart using loops, functions, recursion, and built-in methods, you gain practical skills that are useful for a wide variety of programming tasks. Each approach has its benefits, whether it’s clarity, simplicity, or reusability.

Practicing these programs helps beginners understand how to traverse lists and make decisions based on conditions. Start with loops, try recursion, and explore Dart’s built-in functions. The more you experiment, the more comfortable you’ll become with searching techniques, preparing you for more advanced algorithms in the future.

Scroll to Top