An array of 2000 data entries to find the index with duplicate values

Source: Internet
Author: User
An array of 2000 data entries and an index with duplicate values. for example, an array contains ,............... the repeat indexes, 14 & nbsp; 5, 15 & nbsp ;.... it cannot be a two-for or while loop, which is too costly. ----- an array of 2000 data records, and an index with duplicate values is found.
For example, an array contains ,...............

Obtain 2 duplicate indexes, 2, 3, 10, 11, and 14.
5 duplicate indexes 7, 8, 15
....

It cannot be a two-for or while loop, which is too costly.

------ Solution --------------------
PHP code
$ Arr = array (,); $ tmp = array (); foreach ($ arr AS $ k =>v v) {if (isset ($ tmp [$ v]) {if ($ tmp [$ v]) {$ tmp [$ v]. = "," ;}$ tmp [$ v]. = $ k;} else {$ tmp [$ v] = "" ;}} foreach ($ tmp AS $ k => $ v) {if ($ v) {echo $ k, "=>", $ v, "\ n" ;}} unset ($ tmp );
------ Solution --------------------
PHP code
$ A = array (,); $ r = array (); foreach ($ a as $ v) {if (isset ($ r [$ v]) continue; if ($ t = array_keys (array_intersect ($ a, array ($ v )))) {unset ($ t [0]); $ r [$ v] = join (',', $ t) ;}$ r = array_diff ($ r, array (''); print_r ($ r );
------ Solution --------------------
PHP code
$arr = array(1,2,2,2,3,4,5,5,5,6,2,2,7,8,2,5,3,9);$str = implode(',', $arr);foreach ($arr as $k=>$v){    $t[$v] .= !isset($t[$v]) ? '' : $k . ',';    $l += strlen($v);    if((strrpos($str, $v.'')) == $l-strlen($v))        $t[$v] = trim($t[$v], ',');    ++$l;}print_r($t); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.