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 and should be .
-
The union of and should be .
-
The union of and should be .
The following program unites two arrays, assuming that the operand arrays are sorted. This operation may result in duplicate values.
p = [2,5,5,8,8,8,9,30,45] # Enter the array 1q = [4,5,5,6,7,7,8,9,10] # Enter the array 2lp,lq = p.length,q.length # Calculating the length of both p and qlr = lp + lqr = [0]*lr # creating r of zerosi = j = k = 0while i<lp and j<lq # This loop will terminate when the values of i,j are not less than lp and lqif p[i] < q[j] # If the element of p is less than element of qr[k]=p[i] # Assign the value of p to ri += 1else #If the element of p is not less than element of qif p[i] > q[j] # If the element of p is greater than element of qr[k]=q[j] # Assign the value of q to rj += 1else # The elements of p and q are equalr[k]=p[i]i += 1j += 1endendk += 1endwhile (i<lp) # This loop will terminate when the value of i is not less than lpr[k]=p[i]i+=1k+=1endwhile (j<lq) # This loop will terminate when the value of j is not less than lqr[k]=q[j]j+=1k+=1endprint(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 ofp.length
andlq
stores the result ofq.length
. This may also be done in two assignment statements. - In
i = j = k = 0
, all the variables get the value0
. This can also be done in three assignment statements.
In the rest of the code above:
- The first loop traverses through both arrays (
p
andq
) and puts the smaller value in the resultingr
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 and should be .
-
The intersection of and should be .
-
The intersection of and should be .
The following program assumes that the operand arrays are sorted, and may result in duplicate values:
p = [2,5,5,8,8,8,9,30,45] # Enter array 1q = [4,5,5,6,7,7,8,8,9,10,45] # Enter array 2lp,lq = p.length,q.length # Calculating the length of both p and qif lq<lplr = lqelselr = lpendr = [0]*lr # Creating r of zerosi = j = k = 0while(i<lp and j<lq) # This loop will terminate when the values of i,j are not less than lp and lqif (p[i] == q[j]) # If the element of p is equal to the element of qr[k]=p[i] # Assigning value of p to ki += 1j += 1k += 1else # If the element of p is not equal to the element of qif(p[i] < q[j]) # If the element of p is less than the element of qi += 1else # If the element of p is not less than the element of qj += 1endendendprint(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.
p = [2,5,5,8,8,8,9,30,45] # Enter array 1q = [4,5,5,6,7,7,8,9,10] # Enter array 2lp,lq = p.length,q.length # Calculating the length of both p and qlr = lp + lqr = [0]*lr # Creating r of zerosi = j = k = 0while i<lp and j<lq # This loop will terminate when the values of i,j are not less than lp and lqif (p[i] < q[j]) # If the element of p is less than the element of qr[k]=p[i] # Assigning p to ri += 1k += 1else # If the element of p is not less than the element of qif(p[i] > q[j]) # If the element of p is greater than element of qj += 1else # The elements of p and q are equali += 1j += 1endendendwhile (i<lp)# This loop will terminate when the value of i is not less than lpr[k]=p[i] # Assigning p to ri+=1k+=1endprint(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:
p = [2,5,5,8,8,8,9,30,45] # Enter the arraylp = p.length # Calculating the length of pr = [p[0]]*lp # Creating r of zeroesi = k = 1while i<lp # This loop will terminate when the value of i is not less than lpif (p[i] != p[i-1]) # If the element of p is not equal to the previous elementr[k]=p[i] # Assigning p to kk += 1endi += 1endprint(p,"\n")s = r[0...k]print(s,"\n")