Detect Duplicates in a PHP Array with an O(n) Solution

This article explains how to efficiently detect duplicate elements in a PHP integer array using an O(n) algorithm, detailing the solution class, step‑by‑step execution flow, and its linear time and space complexities.

php Courses
php Courses
php Courses
Detect Duplicates in a PHP Array with an O(n) Solution

Detecting duplicate elements in an array requires traversing each element and checking for repeated occurrences.

Introduction

Given an integer array nums, the function should return true if any value appears at least twice; otherwise it returns false.

Example:

Input: nums = [1,2,3,1]
Output: true

Input: nums = [1,2,3,4]
Output: false

Explore Code

Below is a PHP class Solution with the method containsDuplicate($nums) that implements the detection:

class Solution {

    /**
     * @param Integer[] $nums
     * @return Boolean
     */
    function containsDuplicate($nums) {
        $map = array();
        foreach ($nums as $n => $i) {
            if (array_key_exists($i, $map)) {
                return true;
            }
            $map[$i] = $n;
        }
        return false;
    }
}

Function Execution Flow

1. Initialization: an empty associative array $map is created to store encountered elements and their indices.

2. Duplicate detection:

The function iterates over each element in $nums.

For each element, it checks whether the element already exists in $map.

If it exists, the function immediately returns true, indicating a duplicate.

If not, the element and its index are added to $map for future checks.

3. Return value: after scanning the entire array without finding duplicates, the function returns false, meaning the array has all unique elements.

Time and Space Complexity

Time complexity: O(n), where n is the number of elements in the input array, because each element is examined once.

Space complexity: O(n), as the associative array $map may store up to n unique elements in the worst case.

Conclusion

The optimized PHP solution uses an associative array for constant‑time lookups, enabling fast and reliable detection of duplicate elements while maintaining linear time and space performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

duplicate detectiontime-complexityspace complexityarray algorithm
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.