Linear Search with Duplicates

Linear Search with Duplicates

01 Sep 2025
Beginner
25 Views
3 min read
Learn with an interactive course and practical hands-on labs

Free DSA Online Course with Certification

Linear Search with Duplicates

The Linear Search Algorithm is a simple search algorithm that sequentially checks each element in an array until a match is found or the array is fully traversed. When handling duplicates, the algorithm is modified to return all indices where the target value appears, rather than stopping at the first match. This approach is useful for unsorted arrays or when all occurrences of a value are needed. The time complexity is O(n), where n is the length of the array.

Example

  • Input: nums = [5, 2, 8, 5, 1, 5, 3], target = 5
  • Output: [0, 3, 5]
  • Explanation: The target value 5 appears at indices 0, 3, and 5 in the array.

Logic

Enter an array of numbers (comma-separated) and a target value to find all indices where the target appears using the Linear Search algorithm.

Program (Python - Linear Search with Duplicates)

The following Python code implements the Linear Search algorithm to find all indices of a target value in an array, with O(n) time complexity, where n is the length of the array.


def linearSearchDuplicates(nums, target):
    result = []
    for i in range(len(nums)):
        if nums[i] == target:
            result.append(i)
    return result

# Example usage
nums = [5, 2, 8, 5, 1, 5, 3]
target = 5
print(linearSearchDuplicates(nums, target))  # Output: [0, 3, 5]

Output

For the input nums = [5, 2, 8, 5, 1, 5, 3] and target = 5, the output is:

[0, 3, 5]

Explanation:

The target value 5 is found at indices 0, 3, and 5 in the array.

Take our Datastructures skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Amit Kumar Ghosh (SDE and Mentor at Scholarhat)

As a software developer with a wealth of experience, he brings a unique combination of technical acumen and a passion for mentorship to my role. With 6 years of experience, he has honed the skills in C/C++, Java, Python, SQL, C#, JavaScript, React, Java Spring etc. and has a proven track record of delivering high-quality, scalable software solutions and core Computer fundamental knowledge DSA, OOPs, CN, OS etc.

As a teacher, his approach revolves around interactive techniques, prioritizing hands-on learning and real-world projects. He explains concepts clearly, offer practical examples, and encourage questions to foster a supportive environment.
Live Training - Book Free Demo
ASP.NET Core Certification Training
06 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
06 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI Foundry Certification Training
06 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
React Certification Training
07 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure Developer Certification Training
08 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this