Questions tagged [array-difference]

Used for questions concerning the difference between arrays. For example, where elements in two arrays differ from each other, or subtracting the content of one array from another. Since the tag can be broadly applied to any programming language that supports arrays, use use one or more additional tags in order to help identify the specific language the question relates.

An array is a data structure that encompasses a group of elements. When working with arrays there can be a requirement to determine the difference between two or more arrays, and this tag is for questions relating specifically to finding that difference.

The difference between arrays can be determined in a number of ways, including finding where elements within the array differ, and can include operations such as subtracting the contents of one array from another to produce an outcome.

273 questions
1314
votes
84 answers

How to get the difference between two arrays in JavaScript?

Is there a way to return the difference between two arrays in JavaScript? For example: var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; // need ["c", "d"]
John Adawan
  • 13,329
  • 4
  • 20
  • 11
92
votes
2 answers

Subtracting one Array from another in Ruby

I've got two arrays of Tasks - created and assigned. I want to remove all assigned tasks from the array of created tasks. Here's my working, but messy, code: @assigned_tasks = @user.assigned_tasks @created_tasks = @user.created_tasks …
doctororange
  • 11,670
  • 12
  • 42
  • 58
84
votes
11 answers

"Array to string conversion error" when calling array_diff_assoc() with a multidimensional array

I get array to string conversion error for the following line: $diff = array_diff_assoc($stockist, $arr); Here, $arr is an array decoded from a JSON file. Using the is_array() function I was able to verify that both parameters are arrays. Can…
user2963765
  • 841
  • 1
  • 6
  • 4
67
votes
10 answers

Compare 2 arrays which returns difference

What's the fastest/best way to compare two arrays and return the difference? Much like array_diff in PHP. Is there an easy function or am I going to have to create one via each()? or a foreach loop?
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
51
votes
2 answers

Difference between two numpy arrays in python

I have two arrays, for example: array1=numpy.array([1.1, 2.2, 3.3]) array2=numpy.array([1, 2, 3]) How can I find the difference between these two arrays in Python, to give: [0.1, 0.2, 0.3] As an array as well? Sorry if this is an amateur question…
user3263816
  • 527
  • 1
  • 4
  • 3
46
votes
9 answers

Compare 2-dimensional data sets based on a specified second level value

I have an array containing rows of associative data. $array1 = array( array('ITEM' => 1), array('ITEM' => 2), array('ITEM' => 3), ); I have a second array, also containing rows of associative data, that I would like to filter using the…
40
votes
5 answers

Get the difference of two arrays of objects

I know there is array_diff and array_udiff for comparing the difference between two arrays, but how would I do it with two arrays of objects? array(4) { [0]=> object(stdClass)#32 (9) { ["id"]=> string(3) "205" …
roflwaffle
  • 29,590
  • 21
  • 71
  • 94
21
votes
2 answers

PHP: Case-insensitive "array_diff"

I have following two arrays and the code to find array_diff: $obs_ws = array("you", "your", "may", "me", "my", "etc"); $all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe"); $final_ws = array_diff($all_ws, $obs_ws); Above code giving…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
15
votes
2 answers

Python numpy subtraction no negative numbers (4-6 gives 254)

I wish to subtract 2 gray human faces from each other to see the difference, but I encounter a problem that subtracting e.g. [4] - [6] gives [254] instead of [-2] (or difference: [2]). print(type(face)) # print(face.shape)…
NumesSanguis
  • 5,832
  • 6
  • 41
  • 76
15
votes
3 answers

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

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…
Solace
  • 8,612
  • 22
  • 95
  • 183
12
votes
3 answers

Remove array elements that are present in another array

There is a list of words and list of banned words. I want to go through the word list and redact all the banned words. This is what I ended up doing (notice the catched boolean): puts "Give input text:" text = gets.chomp puts "Give redacted…
Mikko Vedru
  • 333
  • 2
  • 11
11
votes
4 answers

Using array_diff(), how to get difference from array2 instead of array1?

I'm trying to use array_diff like so. Here are my two array outputs: List 1 Output Array ([0] => 0022806 ) List 2 Output Array ([0] => 0022806 [1] => 0023199 ) PHP $diff = array_diff($list_1, $list_2); print "DIFF: " . count($diff)…
user1216398
  • 1,890
  • 13
  • 32
  • 40
8
votes
3 answers

array_diff() with "one-for-one" element removal when there are duplicate array values

I have two arrays containing repeating values: $test1 = [ "blah1", "blah1", "blah1", "blah1", "blah2" ]; $test2 = [ "blah1", "blah1", "blah1", "blah2" ]; I am trying to get array difference: $result =…
Acidon
  • 1,294
  • 4
  • 23
  • 44
7
votes
3 answers

What is the difference between array_udiff_assoc() and array_diff_uassoc()?

What is the difference between array_udiff_assoc() and array_diff_uassoc()? For array_udiff_assoc(), I have this code: function myfunction($v1,$v2) { if ($v1===$v2) { return 0; } return 1; } $a1 = ["a" => "Cat", "b" => "Dog",…
php.khan
  • 1,224
  • 1
  • 12
  • 21
7
votes
6 answers

Compare two arrays and create an associative array of true/false values

I want to compare the values of two flat, indexed arrays and generate a new array where the keys are the original values from the first array and the values are boolean values indicating whether the same value occurred in both origibal…
Luqsus
  • 73
  • 1
  • 5
1
2 3
18 19