NOTE: This solution assumes array1 has no duplicates.

Simple Explanation

The complexity of all these answers obscures understanding the principle. That's unfortunate because the principle is simple:

  • array1 minus array2 returns:
  • everything that's left in array1
  • after removing everything that is in array2
  • (and discarding the rest of array2)

Simple Demo

# From array1, subtract array2, leaving the remainder
$ jq --null-input '[1,2,3,4] - [2,4,6,8]'
[
  1,
  3
]

# Subtract the remainder from the original
$ jq --null-input '[1,2,3,4] - [1,3]'
[
  2,
  4
]

# Put it all together
$ jq --null-input '[1,2,3,4] - ([1,2,3,4] - [2,4,6,8])'
[
  2,
  4
]

comm Demo

def comm:
  (.[0] - (.[0] - .[1])) as d, .[1]-d]
;

With that understanding, I was able to imitate the behavior of the *nix comm command

With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

$ echo 'def comm: (.[0]-(.[0]-.[1])) as d,.[1]-d];' > comm.jq
$ echo '{"a":101, "b":102, "c":103, "d":104}'                        > 1.json
$ echo '{         "b":202,          "d":204, "f":206, "h":208}'      > 2.json

$ jq --slurp '.' 1.json 2.json
[
  {
    "a": 101,
    "b": 102,
    "c": 103,
    "d": 104
  },
  {
    "b": 202,
    "d": 204,
    "f": 206,
    "h": 208
  }
]

$ jq --slurp '[.[] | keys | sort]' 1.json 2.json
[
  [
    "a",
    "b",
    "c",
    "d"
  ],
  [
    "b",
    "d",
    "f",
    "h"
  ]
]

$ jq --slurp 'include "comm"; [.[] | keys | sort] | comm' 1.json 2.json
[
  [
    "a",
    "c"
  ],
  [
    "f",
    "h"
  ],
  [
    "b",
    "d"
  ]
]

$ jq --slurp 'include "comm"; [.[] | keys | sort] | comm[2]' 1.json 2.json
[
  "b",
  "d"
]
Answer from Bruno Bronosky on Stack Overflow
🌐
Rptools
wiki.rptools.info › index.php › json.intersection
json.intersection() - RPTools Wiki
March 15, 2023 - [h: array1 = json.append("",1,2,3,4)] [h: array2 = json.append("",3,4,5,6)] [r: json.intersection(array1,array2)]
🌐
Javadoc.io
javadoc.io › static › com.github.imrafaelmerino › json-values › 4.1.0 › jsonvalues › JsObj.html
JsObj (json-values 4.1.0 API)
So this operation is kind of a 'recursive' intersection. Parameters: that - the other object · ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET · Returns: a new JsObj of the same type as the inputs (mutable or immutable) JsObj union​(JsObj that) returns this json object plus those pairs from the given json object that which keys don't exist in this.
🌐
GitHub
github.com › jqlang › jq › issues › 1181
how to get the intersection of two JSON arrays using jq? · Issue #1181 · jqlang/jq
July 14, 2016 - Posted details at http://stackoverflow.com/questions/38364458/how-to-get-the-intersection-of-two-json-arrays-using-jq/38364583#38364583 and figured why not go straight to the source. Any ideas? Given arrays X and Y (preferably both as in...
Author: jqlang
🌐
MariaDB
jira.mariadb.org › browse › MDEV-26182
[MDEV-26182] Create a function to check for JSON intersection - Jira
July 20, 2021 - JSON_ARRAY_INTERSECT() : Find intersection between two jsons of array type,
Top answer
1 of 5
14

NOTE: This solution assumes array1 has no duplicates.

Simple Explanation

The complexity of all these answers obscures understanding the principle. That's unfortunate because the principle is simple:

  • array1 minus array2 returns:
  • everything that's left in array1
  • after removing everything that is in array2
  • (and discarding the rest of array2)

Simple Demo

# From array1, subtract array2, leaving the remainder
$ jq --null-input '[1,2,3,4] - [2,4,6,8]'
[
  1,
  3
]

# Subtract the remainder from the original
$ jq --null-input '[1,2,3,4] - [1,3]'
[
  2,
  4
]

# Put it all together
$ jq --null-input '[1,2,3,4] - ([1,2,3,4] - [2,4,6,8])'
[
  2,
  4
]

comm Demo

def comm:
  (.[0] - (.[0] - .[1])) as d, .[1]-d]
;

With that understanding, I was able to imitate the behavior of the *nix comm command

With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

$ echo 'def comm: (.[0]-(.[0]-.[1])) as d,.[1]-d];' > comm.jq
$ echo '{"a":101, "b":102, "c":103, "d":104}'                        > 1.json
$ echo '{         "b":202,          "d":204, "f":206, "h":208}'      > 2.json

$ jq --slurp '.' 1.json 2.json
[
  {
    "a": 101,
    "b": 102,
    "c": 103,
    "d": 104
  },
  {
    "b": 202,
    "d": 204,
    "f": 206,
    "h": 208
  }
]

$ jq --slurp '[.[] | keys | sort]' 1.json 2.json
[
  [
    "a",
    "b",
    "c",
    "d"
  ],
  [
    "b",
    "d",
    "f",
    "h"
  ]
]

$ jq --slurp 'include "comm"; [.[] | keys | sort] | comm' 1.json 2.json
[
  [
    "a",
    "c"
  ],
  [
    "f",
    "h"
  ],
  [
    "b",
    "d"
  ]
]

$ jq --slurp 'include "comm"; [.[] | keys | sort] | comm[2]' 1.json 2.json
[
  "b",
  "d"
]
2 of 5
5

A simple and quite fast (but somewhat naive) filter that probably does essentially what you want can be defined as follows:

   # x and y are arrays
   def intersection(x;y):
     ( (x|unique) + (y|unique) | sort) as $sorted
     | reduce range(1; $sorted|length) as sorted[sorted[$i-1] then . + [$sorted[$i]] else . end) ;

If x is provided as input on STDIN, and y is provided in some other way (e.g. def y: ...), then you could use this as: intersection(.;y)

Other ways to provide two distinct arrays as input include:

  • using the --slurp option
  • using --arg a v (or --argjson a v if available in your jq)

Here's a simpler but slower def that's nevertheless quite fast in practice:

    def i(x;y):
       if (y|length) == 0 then []
       else (x|unique) as x - ($x - y)
       end ;

Here's a standalone filter for finding the intersection of arbitrarily many arrays:

# Input: an array of arrays
def intersection:
  def i(y): ((unique + (y|unique)) | sort) as $sorted
  | reduce range(1; $sorted|length) as sorted[sorted[$i-1] then . + [$sorted[$i]] else . end) ;
  reduce .[1:][] as a)) ;

Examples:

[ [1,2,4], [2,4,5], [4,5,6]] #=> [4]
[[]]                         #=> []
[]                           #=> null

Of course if x and y are already known to be sorted and/or unique, more efficient solutions are possible. See in particular Finite Sets of JSON Entities

🌐
GitHub
github.com › jwadhams › json-logic › issues › 42
Add array "intersection" operator · Issue #42 · jwadhams/json-logic
January 12, 2024 - Currently there is no way to find out if there are any common elements in two arrays in the data object. This could be achieved by adding a globally available intersection function which returns the common elements in two arrays: {"inter...
Author: jwadhams
🌐
Baeldung
baeldung.com › home › java › java array › intersection between two integer arrays
Intersection Between Two Integer Arrays| Baeldung
March 17, 2024 - In this article, we’ve seen how to use the contains and remove methods to implement an intersection for two arrays in Java.
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java array › intersection of two arrays in java
Intersection of Two Arrays in Java
February 22, 2023 - Integer[] array1 = new Integer[]{1, 2, 3, 4, 5}; Integer[] array2 = new Integer[]{4, 5, 6, 7}; Integer[] intersection = Arrays.stream(array1) .distinct() .filter(x -> Arrays.asList(array2).contains(x)) .toArray(Integer[]::new); Assertions.assertArrayEquals(new Integer[]{4, 5}, intersection); Happy Learning !! ... The code is same. We only need to decide the equals() method implementation in the Object. Comments are closed. ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
🌐
Medium
medium.com › @shaswata.ssaha › efficiently-finding-the-intersection-of-two-arrays-in-java-ce8b6c08c0ec
Efficiently Finding the Intersection of Two Arrays in Java | by Shaswata Saha | Medium
July 2, 2024 - Find Intersection: Iterate over nums2 and check if the element exists in the HashMap with a count greater than zero. If it does, add it to the result list and decrement the count in the map.
🌐
GitHub
github.com › sa1123 › Coding-Ninjas-Java › blob › main › Array_Intersection.java
Coding-Ninjas-Java/Array_Intersection.java at main · sa1123/Coding-Ninjas-Java
You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.
Author: sa1123
🌐
LeetCode
leetcode.com › problems › intersection-of-two-arrays
Intersection of Two Arrays - LeetCode
Can you solve this real interview question? Intersection of Two Arrays - Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
🌐
TutorialsPoint
tutorialspoint.com › how-to-find-the-intersection-of-two-arrays-in-java
How to find the intersection of two arrays in java?
To find the intersection of two arrays in java use two loops. The outer loop is to iterate the elements of the first array whereas, the second loop is to iterate the elements of the second array.
🌐
Sololearn
sololearn.com › en › Discuss › 1627015 › intersection-array-from-two-arrays-java
Intersection array from two arrays, Java. | Sololearn: Learn to code for FREE!
There are some errors in your code 1. for (int i = 0; i < arr.length; i++) { ===> for (int i = 0; i < arr1.length; i++) { 2. if (list[i] == list1[j]) { ==> if (arr1[i] == arr2[j]) { 3. common[k] = list[i]; ==> common[k] = arr1[i]; 4. Add one more } at the end Nevertheless the problem still exists. Numbers are duplicated in the array. I believe that you want the distincts. https://code.sololearn.com/cvi3k6mqfm12/#java Keep in mind that in the above code I use 4 positions as a size of common array.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › intersection-of-two-arrays
Intersection of two Arrays - GeeksforGeeks
Given two arrays a[] and b[], find their intersection — the unique elements that appear in both.
Published: July 27, 2025
🌐
MariaDB
mariadb.com › docs › server › reference › sql-functions › special-functions › json-functions › json_array_intersect
JSON_ARRAY_INTERSECT | Server | MariaDB Documentation
Available from version 11.2, this function compares two JSON arrays and returns a new array containing only the items present in both.