How can the code be optimized to prevent the duplication of values in the input fields?

To prevent duplication of values in input fields, we can utilize an array to keep track of the values that have already been entered. Before adding a new value to the input field, we can check if it already exists in the array. If it does, we can display an error message or prevent the duplicate value from being added.

<?php
$values = [];

if(isset($_POST['submit'])){
    $input_value = $_POST['input_field'];

    if(in_array($input_value, $values)){
        echo "Error: Value already exists!";
    } else {
        $values[] = $input_value;
        // Process the input value further if needed
    }
}
?>

<form method="post" action="">
    <input type="text" name="input_field">
    <input type="submit" name="submit" value="Submit">
</form>