PHP Program to Find the Number Occurring Odd Number of Times

王林
Release: 2024-08-28 12:30:42
Original
1003 people have browsed it

PHP Program to Find the Number Occurring Odd Number of Times

What is PHP?

PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. It allows developers to embed code within HTML files, enabling the creation of dynamic web pages and interactions with databases. PHP is known for its simplicity, versatility, and extensive integration capabilities with popular databases. It offers a broad range of extensions and has a large community of developers, ensuring ample resources and support.

PHP Program to Find the Number Occurring Odd Number of Times

The concept of "Number Occurring Odd Number of Times" refers to finding a number in an array that appears an odd number of times, while all other numbers appear an even number of times. In other words, there is only one number in the array that has an odd count, while all other numbers have an even count.

Example

Let's take an example to illustrate this concept:

Consider the following array: [2, 3, 4, 3, 1, 4, 2, 1, 1]

In this array, all numbers except for the number 1 appear an even number of times. The number 1 appears 3 times, which is an odd count. Therefore, the number 1 is the number occurring an odd number of times in this array.

This program can be implemented using various approaches such as hashing, bitwise operations, or sorting.

Method 1- Using sorting

<?php

function findOddNumber($arr) {
   $count = array();

   foreach($arr as $num) {
      if(isset($count[$num])) {
         $count[$num]++;
      } else {
         $count[$num] = 1;
      }
   }

   foreach($count as $num => $occurrences) {
      if($occurrences % 2 != 0) {
         return $num;
      }
   }

   return -1; // If no number occurs an odd number of times
}

// Example usage

$arr = array(5, 7, 2, 7, 5, 2, 1, 1, 9, 9, 9);
$oddNumber = findOddNumber($arr);

if($oddNumber != -1) {

   echo "The number occurring an odd number of times is: " . $oddNumber;
} else {

   echo "No number occurs an odd number of times in the array.";
}
?>
Copy after login

Output

The number occurring an odd number of times is: 9
Copy after login

Method 2- Using Hashing

<?php
function findOddNumber($arr) {
   $hash = array();
   foreach($arr as $num) {
      if(isset($hash[$num])) {
         $hash[$num]++;
      } else {
         $hash[$num] = 1;
      }
   }
   foreach($hash as $num => $occurrences) {
      if($occurrences % 2 != 0) {
         return $num;
      }
   }
   return -1; // If no number occurs an odd number of times
}  
// Example usage
$arr = array(2, 3, 4, 3, 1, 4, 2, 1, 1);
$oddNumber = findOddNumber($arr);
if($oddNumber != -1) {
   echo "The number occurring an odd number of times is: " . $oddNumber;
} else {
   echo "No number occurs an odd number of times in the array.";
}
?>
Copy after login

Output

The number occurring an odd number of times is: 1
Copy after login

Method 3 - Using bitwise XOR operations.

<?php
function odd_occurrence($arr)
{
   $result = 0;

   # Traverse the array
   foreach ($arr as &$value)
   {
      # Xor (exclusive or)
      # Bits that are set in $a or $b but not both are set.
      $result = $result ^ $value;
   }
   return $result;
}
$num1 = array( 3, 5, 6, 2, 3, 6, 2, 5, 7);
print_r(odd_occurrence($num1)."<br>");
?>
Copy after login

Output

7
Copy after login

Conclusion

In Conclusion, The PHP program efficiently identifies the number occurring an odd number of times in an array. It provides a reliable solution for various applications and algorithms. By iterating through the array and keeping track of the count for each number, the program accurately identifies the number with an odd count.

The PHP program to find the number occurring an odd number of times is an efficient solution that utilizes the concept of hashing. It takes an input array and uses a hash table to store the count of each number. By iterating over the hash table, it identifies the number with an odd count, indicating the number occurring an odd number of times in the array. Using the hashing technique, the program achieves a time complexity of O(n), where n is the size of the input array. This makes it an optimal solution for finding the number occurring an odd number of times in an array, providing a reliable tool for various applications and algorithms.

The program can utilize bitwise XOR operations to find the number occurring an odd number of times. By performing XOR operations on all elements in the array, the program can extract the unique number efficiently.

The above is the detailed content of PHP Program to Find the Number Occurring Odd Number of Times. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template