Programs of Arrays Operations

Find distinct elements and other operations on the arrays.

The union of two arrays

The union operation combines two sets, skipping the matching values of both sets while populating the resultant set.

Unlike sets, arrays may contain duplicate values. For example:

  • The union of [2,5,8,9,30,45][2,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [2,4,5,8,9,10,30,45][2,4,5,8,9,10,30,45].

  • The union of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [2,4,5,5,8,9,10,30,45][2,4,5,5,8,9,10,30,45].

  • The union of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,5,6,7,8,9,10][4,5,5,6,7,8,9,10] should be [2,4,5,5,8,9,10,30,45][2,4,5,5,8,9,10,30,45].

The following program unites two arrays, assuming that the operand arrays are sorted. This operation may result in duplicate values.

Press + to interact
p = [2,5,5,8,8,8,9,30,45] # Enter the array 1
q = [4,5,5,6,7,7,8,9,10] # Enter the array 2
lp,lq = p.length,q.length # Calculating the length of both p and q
lr = lp + lq
r = [0]*lr # creating r of zeros
i = j = k = 0
while i<lp and j<lq # This loop will terminate when the values of i,j are not less than lp and lq
if p[i] < q[j] # If the element of p is less than element of q
r[k]=p[i] # Assign the value of p to r
i += 1
else #If the element of p is not less than element of q
if p[i] > q[j] # If the element of p is greater than element of q
r[k]=q[j] # Assign the value of q to r
j += 1
else # The elements of p and q are equal
r[k]=p[i]
i += 1
j += 1
end
end
k += 1
end
while (i<lp) # This loop will terminate when the value of i is not less than lp
r[k]=p[i]
i+=1
k+=1
end
while (j<lq) # This loop will terminate when the value of j is not less than lq
r[k]=q[j]
j+=1
k+=1
end
print(p,"\n")
print(q,"\n")
s = r[0...k]
print(s,"\n")

The above code features two new methods of assignment in Ruby.

  • In lp , lq = p.length , q.length, the two variables on the LHS of the assignment operator are assigned the two values or results on the RHS of the same. So, lp gets the value of p.length and lq stores the result of q.length. This may also be done in two assignment statements.
  • In i = j = k = 0, all the variables get the value 0. This can also be done in three assignment statements.

In the rest of the code above:

  • The first loop traverses through both arrays (p and q) and puts the smaller value in the resulting r array. After completing this round, any one array is exhausted.
  • The other array still has one or more untraversed values, all of which are certainly greater than or equal to the last value of the exhausted array.
  • Therefore, one of the next two loops executes to add those values to the end of r without comparison.

The intersection of two arrays

The intersection operation gives the matching values of two arrays. Unlike sets, arrays may contain duplicate values. For example:

  • The intersection of [2,5,8,9,30,45][2,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [5,8,9][5,8,9].

  • The intersection of [2,5,5,6,8,9,30,45][2,5,5,6,8,9,30,45] and [4,5,6,7,8,8,9,10][4,5,6,7,8,8,9,10] should be [5,6,8,9][5,6,8,9].

  • The intersection of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,5,6,7,8,9,10][4,5,5,6,7,8,9,10] should be [5,5,8,9][5,5,8,9].

The following program assumes that the operand arrays are sorted, and may result in duplicate values:

Press + to interact
p = [2,5,5,8,8,8,9,30,45] # Enter array 1
q = [4,5,5,6,7,7,8,8,9,10,45] # Enter array 2
lp,lq = p.length,q.length # Calculating the length of both p and q
if lq<lp
lr = lq
else
lr = lp
end
r = [0]*lr # Creating r of zeros
i = j = k = 0
while(i<lp and j<lq) # This loop will terminate when the values of i,j are not less than lp and lq
if (p[i] == q[j]) # If the element of p is equal to the element of q
r[k]=p[i] # Assigning value of p to k
i += 1
j += 1
k += 1
else # If the element of p is not equal to the element of q
if(p[i] < q[j]) # If the element of p is less than the element of q
i += 1
else # If the element of p is not less than the element of q
j += 1
end
end
end
print(p,"\n")
print(q,"\n")
s = r[0...k]
print(s,"\n")

Note: If the operand arrays are not sorted already, then the code for sorting them should be added before entering the first loop.

The minus operator for two arrays

The following program implements the minus operator for arrays similar to sets. It’s assumed that the operand arrays are sorted, and the resulting arrays may contain duplicates, depending upon the contents of the operands.

Press + to interact
p = [2,5,5,8,8,8,9,30,45] # Enter array 1
q = [4,5,5,6,7,7,8,9,10] # Enter array 2
lp,lq = p.length,q.length # Calculating the length of both p and q
lr = lp + lq
r = [0]*lr # Creating r of zeros
i = j = k = 0
while i<lp and j<lq # This loop will terminate when the values of i,j are not less than lp and lq
if (p[i] < q[j]) # If the element of p is less than the element of q
r[k]=p[i] # Assigning p to r
i += 1
k += 1
else # If the element of p is not less than the element of q
if(p[i] > q[j]) # If the element of p is greater than element of q
j += 1
else # The elements of p and q are equal
i += 1
j += 1
end
end
end
while (i<lp)# This loop will terminate when the value of i is not less than lp
r[k]=p[i] # Assigning p to r
i+=1
k+=1
end
print(p,"\n")
print(q,"\n")
s = r[0...k]
print(s,"\n")

Distinct values in a array

The following program generates a new arrays with the unique values of the operand arrays:

Press + to interact
p = [2,5,5,8,8,8,9,30,45] # Enter the array
lp = p.length # Calculating the length of p
r = [p[0]]*lp # Creating r of zeroes
i = k = 1
while i<lp # This loop will terminate when the value of i is not less than lp
if (p[i] != p[i-1]) # If the element of p is not equal to the previous element
r[k]=p[i] # Assigning p to k
k += 1
end
i += 1
end
print(p,"\n")
s = r[0...k]
print(s,"\n")