array-intersect

Array intersection function speed

ぃ、小莉子 提交于 2020-02-05 05:57:05
问题 I've written a short function for array intersection and wanted to know why one function is faster than the other. 1) Dim list2() As String 'Assume it has values' Dim list2length As Integer = list2.length Function newintersect(ByRef list1() As String) As String() Dim intersection As New ArrayList If (list1.Length < list2length) Then 'use list2' For Each thing As String In list2 If (Array.IndexOf(list1, thing) <> -1) Then intersection.Add(thing) End If Next Else 'use list1' For Each thing As

PHP Array_intersect on multidimensional array with unknown number of keys

匆匆过客 提交于 2020-01-04 02:33:06
问题 I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country. I've already made the part where I look up the people that meet the criteria given by the user. These results are stored in a multidimensional array. If the user is looking for someone with a specific resource with a job title and a specific skill the

PHP: in_array() vs array_intersect() performance

痴心易碎 提交于 2020-01-02 03:50:30
问题 What, and how much, is faster - manually iterating over an array with foreach and checking for needle occurrence with in_array() , or using array_intersect() ? 回答1: Benchmark Test Script <?php $numbers = range(32, 127); $numbersLetters = array_map('chr', $numbers); for (;;) { $numbersLetters = array_merge($numbersLetters, $numbersLetters); if (count($numbersLetters) > 10000) { break; } } $numbers = range(1, count($numbersLetters)); printf("Sample size: %d elements in 2 arrays (%d total) \n",

Fast count() intersection of two string arrays

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 03:17:34
问题 I need to count the number of elements corresponding to the intersection of two big arrays of strings and do it very fast. I am using the following code: arr1[i].Intersect(arr2[j]).Count() For CPU Time, VS Profiler indicates 85.1% in System.Linq.Enumerable.Count() 0.3% in System.Linq.Enumerable.Intersect() Unfortunately it might to take hours to do all work. How to do it faster? 回答1: You can use HashSet with arr2 HashSet<string> arr2Set = new HashSet<string>(arr2); arr1.Where(x=>arr2Set

PHP array_intersect case-insensitive and ignoring tildes

烂漫一生 提交于 2020-01-01 12:21:47
问题 Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes? The array_intersect PHP function compares array elements with === so I do not get the expected result. For example, I want this code : $array1 = array("a" => "gréen", "red", "blue"); $array2 = array("b" => "green", "yellow", "red"); $result = array_intersect($array1, $array2); print_r($result); Outputs gréen and red . In default array_intersect function just red is proposed (normal cause

rails - Finding intersections between multiple arrays

我只是一个虾纸丫 提交于 2019-12-31 08:14:11
问题 I am trying to find the intersection values between multiple arrays. for example code1 = [1,2,3] code2 = [2,3,4] code3 = [0,2,6] So the result would be 2 I know in PHP you can do this with array_intersect I wanted to be able to easily add additional array so I don't really want to use multiple loops Any ideas ? Thanks, Alex 回答1: Use the & method of Array which is for set intersection. For example: > [1,2,3] & [2,3,4] & [0,2,6] => [2] 回答2: If you want a simpler way to do this with an array of

rails - Finding intersections between multiple arrays

一笑奈何 提交于 2019-12-31 08:11:13
问题 I am trying to find the intersection values between multiple arrays. for example code1 = [1,2,3] code2 = [2,3,4] code3 = [0,2,6] So the result would be 2 I know in PHP you can do this with array_intersect I wanted to be able to easily add additional array so I don't really want to use multiple loops Any ideas ? Thanks, Alex 回答1: Use the & method of Array which is for set intersection. For example: > [1,2,3] & [2,3,4] & [0,2,6] => [2] 回答2: If you want a simpler way to do this with an array of

PHP array_intersect + array_flip with array that has values multiple times

梦想的初衷 提交于 2019-12-30 11:08:07
问题 I have two arrays: $arr1 = array(101 => 250, 102 => 250, 103 => 250, 104 => 500, 105 => 500, 106 => 500,); and $arr2 = array(0 => 103, 1 => 104, 2 => 105) The result I want to get is Array (103 => 250, 104 => 500) I have tried working with array_intersect(array_flip($arr1), $arr2); but array_flip($arr1) gives something like Array(103 => 250, 106 => 500) thus, keys get lost and can not be intersected correctly. Is there a way to get the desired result? 回答1: The following code does the job. I

PHP: How to compare keys in one array with values in another, and return matches?

自闭症网瘾萝莉.ら 提交于 2019-12-30 04:29:05
问题 I have the following two arrays: $array_one = array('colorZero'=>'black', 'colorOne'=>'red', 'colorTwo'=>'green', 'colorThree'=>'blue', 'colorFour'=>'purple', 'colorFive'=>'golden'); $array_two = array('colorOne', 'colorTwo', 'colorThree'); I want an array from $array_one which only contains the key-value pairs whose keys are members of $array_two (either by making a new array or removing the rest of the elements from $array_one ) How can I do that? I looked into array_diff and array

How to get array intersect with single array group

和自甴很熟 提交于 2019-12-25 07:59:49
问题 I have array values in single array and I need to intersect the arrays inside the main array. Here is my code: $a[1] = array ( 'value' => 'America','value1' => 'England1','value2' => 'Australia','value3' => 'England','value4' => 'Canada', ); $a[2] = array ( 'value' => 'America','value1' => 'Wales','value2' => 'Australia','value3' => 'England1','value4' => 'Canada', ); $a[3] = array ( 'value' => 'America','value1' => 'England','value2' => 'Australia','value3' => 'England1','value4' => 'Canada'