Write a C++ program to find the missing element from two given arrays of integers except one element

The Array object has the following methods:

concat() joins two or more arrays and returns a new array.

let myArray = new Array('1', '2', '3') myArray = myArray.concat('a', 'b', 'c')

join(delimiter = ',') joins all elements of an array into a string.

let myArray = new Array('Wind', 'Rain', 'Fire') let list = myArray.join(' - ')

push() adds one or more elements to the end of an array and returns the resulting length of the array.

let myArray = new Array('1', '2') myArray.push('3')

pop() removes the last element from an array and returns that element.

let myArray = new Array('1', '2', '3') let last = myArray.pop()

shift() removes the first element from an array and returns that element.

let myArray = new Array('1', '2', '3') let first = myArray.shift()

unshift() adds one or more elements to the front of an array and returns the new length of the array.

let myArray = new Array('1', '2', '3') myArray.unshift('4', '5')

slice(start_index, up_to_index) extracts a section of an array and returns a new array.

let myArray = new Array('a', 'b', 'c', 'd', 'e') myArray = myArray.slice(1, 4)

splice(index, count_to_remove, addElement1, addElement2, ...) removes elements from an array and (optionally) replaces them. It returns the items which were removed from the array.

let myArray = new Array('1', '2', '3', '4', '5') myArray.splice(1, 3, 'a', 'b', 'c', 'd')

reverse() transposes the elements of an array, in place: the first array element becomes the last and the last becomes the first. It returns a reference to the array.

let myArray = new Array('1', '2', '3') myArray.reverse()

sort() sorts the elements of an array in place, and returns a reference to the array.

let myArray = new Array('Wind', 'Rain', 'Fire') myArray.sort()

sort() can also take a callback function to determine how array elements are compared.

The sort method (and others below) that take a callback are known as iterative methods, because they iterate over the entire array in some fashion. Each one takes an optional second argument called thisObject. If provided, thisObject becomes the value of the this keyword inside the body of the callback function. If not provided, as with other cases where a function is invoked outside of an explicit object context, this will refer to the global object (window) when using arrow function as callback, or undefined when using normal function as callback.

The callback function is called with two arguments, that are array's elements.

The function below compares two values and returns one of three values:

For instance, the following will sort by the last letter of a string:

let sortFn = function(a, b) { if (a[a.length - 1] < b[b.length - 1]) return -1; if (a[a.length - 1] > b[b.length - 1]) return 1; if (a[a.length - 1] == b[b.length - 1]) return 0; } myArray.sort(sortFn)

  • if a is less than b by the sorting system, return -1 (or any negative number)
  • if a is greater than b by the sorting system, return 1 (or any positive number)
  • if a and b are considered equivalent, return 0.

indexOf(searchElement[, fromIndex]) searches the array for searchElement and returns the index of the first match.

let a = ['a', 'b', 'a', 'b', 'a'] console.log(a.indexOf('b')) console.log(a.indexOf('b', 2)) console.log(a.indexOf('z'))

lastIndexOf(searchElement[, fromIndex]) works like indexOf, but starts at the end and searches backwards.

let a = ['a', 'b', 'c', 'd', 'a', 'b'] console.log(a.lastIndexOf('b')) console.log(a.lastIndexOf('b', 4)) console.log(a.lastIndexOf('z'))

forEach(callback[, thisObject]) executes callback on every array item and returns undefined.

let a = ['a', 'b', 'c'] a.forEach(function(element) { console.log(element) })

map(callback[, thisObject]) returns a new array of the return value from executing callback on every array item.

let a1 = ['a', 'b', 'c'] let a2 = a1.map(function(item) { return item.toUpperCase() }) console.log(a2)

filter(callback[, thisObject]) returns a new array containing the items for which callback returned true.

let a1 = ['a', 10, 'b', 20, 'c', 30] let a2 = a1.filter(function(item) { return typeof item === 'number'; }) console.log(a2)

every(callback[, thisObject]) returns true if callback returns true for every item in the array.

function isNumber(value) { return typeof value === 'number' } let a1 = [1, 2, 3] console.log(a1.every(isNumber)) let a2 = [1, '2', 3] console.log(a2.every(isNumber))

some(callback[, thisObject]) returns true if callback returns true for at least one item in the array.

function isNumber(value) { return typeof value === 'number' } let a1 = [1, 2, 3] console.log(a1.some(isNumber)) let a2 = [1, '2', 3] console.log(a2.some(isNumber)) let a3 = ['1', '2', '3'] console.log(a3.some(isNumber))

reduce(callback[, initialValue]) applies callback(accumulator, currentValue[, currentIndex[, array]]) for each value in the array for the purpose of reducing the list of items down to a single value. The reduce function returns the final value returned by callback function.

If initialValue is specified, then callback is called with initialValue as the first parameter value and the value of the first item in the array as the second parameter value.

If initialValue is not specified, then callback's first two parameter values will be the first and second elements of the array. On every subsequent call, the first parameter's value will be whatever callback returned on the previous call, and the second parameter's value will be the next value in the array.

If callback needs access to the index of the item being processed, or access to the entire array, they are available as optional parameters.

let a = [10, 20, 30] let total = a.reduce(function(accumulator, currentValue) { return accumulator + currentValue }, 0) console.log(total)

reduceRight(callback[, initialValue]) works like reduce(), but starts with the last element.

reduce and reduceRight are the least obvious of the iterative array methods. They should be used for algorithms that combine two values recursively in order to reduce a sequence down to a single value.