a = [1,2,3,4,4] b = [3,4,5,6,7] p a | b # 和集合 [1,2,3,4,5,6,7] p a - b # 差集合 [1,2] p a & b # 積集合 [3,4] var _ = require('lodash'); var a = [1,2,3,4,4] var b = [3,4,5,6,7] console.log(_.union(a, b)); // 和集合(union) [1,2,3,4,5,6,7] console.log(_.difference(a, b)); // 差集合(difference set) [1,2] console.log(_.intersection(a, b)); // 積集合(intersection) [3,4]
![RubyやJavaScriptでの配列の差集合・和集合・積集合 - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/5909da6b2865472003bb2f06c8e6a9d69ce25e06/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-9f5428127621718a910c8b63951390ad.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTkxNiZoPTMzNiZ0eHQ9UnVieSVFMyU4MiU4NEphdmFTY3JpcHQlRTMlODElQTclRTMlODElQUUlRTklODUlOEQlRTUlODglOTclRTMlODElQUUlRTUlQjclQUUlRTklOUIlODYlRTUlOTAlODglRTMlODMlQkIlRTUlOTIlOEMlRTklOUIlODYlRTUlOTAlODglRTMlODMlQkIlRTclQTklOEQlRTklOUIlODYlRTUlOTAlODgmdHh0LWNvbG9yPSUyMzIxMjEyMSZ0eHQtZm9udD1IaXJhZ2lubyUyMFNhbnMlMjBXNiZ0eHQtc2l6ZT01NiZ0eHQtY2xpcD1lbGxpcHNpcyZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZzPWIwYjI0MjczMWIxZDBhNWJlODVmMzRiZmM3OWI1MTFi%26mark-x%3D142%26mark-y%3D112%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTYxNiZ0eHQ9JTQwdGFpem8mdHh0LWNvbG9yPSUyMzIxMjEyMSZ0eHQtZm9udD1IaXJhZ2lubyUyMFNhbnMlMjBXNiZ0eHQtc2l6ZT0zNiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZzPWZiNjk1YzUxN2M5ZGExNDRiZmMxMzIzOGM1NGY0NTlm%26blend-x%3D142%26blend-y%3D491%26blend-mode%3Dnormal%26s%3De845b28f8f25d6f57ea013cca3b22c37)