Find Uncommon Characters of Two Strings in C++

Ayush Gupta
Updated on 19-Aug-2020 10:36:14

203 Views

In this tutorial, we will be discussing a program to find uncommon characters of the two strings.For this we will be provided with two strings. Our task is to print out the uncommon characters of both strings in sorted order.Example Live Demo#include using namespace std; const int LIMIT_CHAR = 26; //finding the uncommon characters void calculateUncommonCharacters(string str1, string str2) {    int isthere[LIMIT_CHAR];    for (int i=0; i

Find Two Distinct Prime Numbers with Given Product in C++

Ayush Gupta
Updated on 19-Aug-2020 10:34:42

194 Views

In this tutorial, we will be discussing a program to find two distinct prime numbers with given product.For this we will be provided with an integer value. Our task is to find the two prime integer values such that their product is equal to the given value.Example Live Demo#include using namespace std; //generating prime numbers less than N. void findingPrimeNumbers(int n, bool calcPrime[]) {    calcPrime[0] = calcPrime[1] = false;    for (int i = 2; i

Check If Object Contains All Keys in JavaScript Array

AmitDiwan
Updated on 19-Aug-2020 07:23:48

584 Views

We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array.So, let’s write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that’s not a key of object, we exit and return false, otherwise we return true.Here is the code for doing the same −Exampleconst obj = { ... Read More

Get Product of Two Integers Without Using JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:21:19

357 Views

We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator.Trick 1: Using Divide Operator TwiceWe know that multiplication and division are just the inverse of each other, so if we divide a number by other number’s inverse, won’t it be same as multiplying the two numbers?Let’s see the code for this −const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b));Trick 2: Using LogarithmsLet’s examine the properties of logarithms first −log(a) + log(b) = log(ab)So, let’s use this ... Read More

Remove Number Properties from an Object in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:19:58

301 Views

We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data type in JavaScript like number, string, object, boolean, symbol etc.Our task is to delete every property of type specified by the second argument. If the second argument is not provided, take ‘number’ as default.The full code for doing so will be −const obj = {    name: 'Lokesh Rahul',   ... Read More

Compute the Sum of Elements of an Array in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:17:58

819 Views

Let’s say, we have an array of arrays, each containing some numbers along with some undefined and null values. We are required to create a new array that contains the sum of each corresponding sub array elements as its element. And the values undefined and null should be computed as 0.Following is the sample array −const arr = [[    12, 56, undefined, 5 ], [    undefined, 87, 2, null ], [    3, 6, 32, 1 ], [    undefined, null ]];The full code for this problem will be −Exampleconst arr = [[    12, 56, undefined, 5 ... Read More

Replace All Characters in a String Except Specific Ones in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:16:29

422 Views

Let’s say, we have to write a function −replaceChar(str, arr, [char])Now, replace all characters of string str that are not present in array of strings arr with the optional argument char. If char is not provided, replace them with ‘*’.Let’s write the code for this function.The full code will be −Exampleconst arr = ['a', 'e', 'i', 'o', 'u']; const text = 'I looked for Mary and Samantha at the bus station.'; const replaceChar = (str, arr, char = '*') => {    const replacedString = str.split("").map(word => {       return arr.includes(word) ? word : char;    }).join("");   ... Read More

Sorting Objects According to Day's Name in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:15:03

2K+ Views

Let’s say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday.Following is our array −const weather = [{    day: 'Wednesday',    humidity: 60 }, {    day: 'Saturday',    humidity: 50 }, {    day: 'Thursday',    humidity: 65 }, {    day: 'Monday',    humidity: 40 }, {    day: 'Sunday',    humidity: ... Read More

Find Duplicate Element in a Progression of First N Terms in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:13:15

176 Views

Let’s say, we are given an array of numbers that contains first n natural numbers, but one element appears twice in the array, so the total number of elements is n+1. Our job is to write a function that takes in the array and returns the number that appears twice in linear time.Method 1: Using Array.prototype.reduce()This is a bit trickier approach but the most compressed in terms of code written. First, let’s see the code for it −const arr = [1, 4, 8, 5, 6, 7, 9, 2, 3, 7]; const duplicate = a => a.reduce((acc, val, ind) => val+acc- ... Read More

Find Highest and Lowest in an Array using JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:11:58

462 Views

We are required to write a function that takes in an array of numbers and returns the difference between its highest and lowest number.At first, create an array −const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454];Now, find maximum and minimum values with Math.max() and Math.min() methods, respectively −const arrayDifference = (arr) => {    let min, max;    arr.forEach((num, index) => {       if(index === 0){          min = num;          max = num;       }else{          min = Math.min(num, min); ... Read More

Advertisements