Rumah > pembangunan bahagian belakang > tutorial php > Program PHP untuk Mencari Nombor Berlaku Bilangan Ganjil Kali

Program PHP untuk Mencari Nombor Berlaku Bilangan Ganjil Kali

王林
Lepaskan: 2024-08-28 12:30:42
asal
1021 orang telah melayarinya

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.";
}
?>
Salin selepas log masuk

Output

The number occurring an odd number of times is: 9
Salin selepas log masuk

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.";
}
?>
Salin selepas log masuk

Output

The number occurring an odd number of times is: 1
Salin selepas log masuk

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>");
?>
Salin selepas log masuk

Output

7
Salin selepas log masuk

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.

Atas ialah kandungan terperinci Program PHP untuk Mencari Nombor Berlaku Bilangan Ganjil Kali. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
php
sumber:tutorialspoint.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan