Skip to content

Commit c74ed0f

Browse files
committed
增加了find_unique
1 parent 712de4b commit c74ed0f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
- [series_sum:规律计算题](https://github.com/uuk020/PHPCode/blob/master/series_sum/answer.php)
1818
- [thirt:13的可分性规则](https://github.com/uuk020/PHPCode/blob/master/thirt/answer.php)
1919
- [listSquared:找出一个数是它所有整除的数的平方之和的结果,此结果能够开平方.](https://github.com/uuk020/PHPCode/blob/master/listSquared/answer.php)
20-
20+
- [Find the unique number: 找数组不同的值.](https://github.com/uuk020/PHPCode/blob/master/find_uniq/answer.php)

find_uniq/answer.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* There is an array with some numbers. All numbers are equal except for one. Try to find it!
4+
* findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
5+
* findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55
6+
*
7+
* @param array $a
8+
* @return void
9+
*/
10+
function find_uniq($a) {
11+
// 排序从小到大
12+
sort($a);
13+
// 比较
14+
return $a[0] === $a[1] ? end($a) : $a[0];
15+
}
16+

0 commit comments

Comments
 (0)