πŸ“˜ Day 6: Linear Search Algorithm in JavaScript

Linear Search (also called sequential search) is the simplest search algorithm. It goes through each element of an array one by one until it finds the target value or reaches the end of the list.


🧠 Real-life Analogy

Imagine you are looking for a friend's name in a guest list β€” so you start checking from the top and keep moving downward until you find it. That’s how linear search works!


πŸ› οΈ How Linear Search Works

  1. Start from the first element.

  2. Compare it with the target.

  3. If it matches, return the index.

  4. If not, move to the next element.

  5. If the target is not found till the end, return -1.


πŸ’» Code Example

javascriptCopyEditfunction linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i; // Found, return index
    }
  }
  return -1; // Not found
}

// Example usage
const numbers = [10, 22, 5, 7, 90, 33];
const result = linearSearch(numbers, 90);

console.log(result); // Output: 4

⏱️ Time & Space Complexity

CaseTime Complexity
Best CaseO(1)
Worst CaseO(n)
SpaceO(1)

πŸ’‘ Best case is when the element is at the beginning of the array.


  • The list is unsorted.

  • The list is short.

  • You want simplicity over speed.

For larger, sorted datasets, binary search is much more efficient β€” and we’ll cover that soon πŸ˜‰


πŸ“š Practice Questions

Here are 3 questions to reinforce today’s topic:


βœ… Easy: Find the Index of a Value

Write a function to find the index of 25 in this array:
[11, 13, 15, 25, 36]

Solution:

javascriptCopyEditfunction findIndex(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }
  return -1;
}

console.log(findIndex([11, 13, 15, 25, 36], 25)); // Output: 3

🧠 Intermediate: Search for a Name

Create a function that checks if the name "Alice" exists in a list of names. Return true or false.

Solution:

javascriptCopyEditfunction containsName(arr, name) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === name) {
      return true;
    }
  }
  return false;
}

console.log(containsName(["Bob", "Eve", "Alice", "Tom"], "Alice")); // Output: true

πŸ”₯ Hard: Count How Many Times a Number Appears

Write a function that counts how many times 3 appears in the array:
[3, 1, 4, 3, 5, 3, 7]

Solution:

javascriptCopyEditfunction countOccurrences(arr, target) {
  let count = 0;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      count++;
    }
  }
  return count;
}

console.log(countOccurrences([3, 1, 4, 3, 5, 3, 7], 3)); // Output: 3

πŸš€ Conclusion

Linear search is easy to implement and works well for small or unsorted lists. It's not the fastest, but it’s reliable and simple β€” a perfect start for every DSA learner.

πŸ“… See you tomorrow for Day 7: Binary Search Algorithm β€” where things get faster and smarter πŸ’‘

#CodeWithGift #DSA2025 #JavaScript #DaySix

1
Subscribe to my newsletter

Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

God'sgift Samuel
God'sgift Samuel

About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.