Help us understand the problem. What is going on with this article?

RubyやJavaScriptでの配列の差集合・和集合・積集合

More than 5 years have passed since last update.

ruby の場合、配列の和集合、差集合、積集合は演算子で扱える。

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]

JavaScriptだとlodashに、それぞれの関数があったりする。

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]

英単語をキチンと覚えておくの大事。

taizo
ruby,javascript,go とかウェブサービスに関わる小さな話を書いていきます。 最近はGoばかりやってます。基本はメモ。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away