We are given with an integer number and the task is to count the even numbers and the odd numbers in a digit. Also, we will keep check on whether the even digits in an integer are occurring an even number of times and also the odd digits in an integer are occurring an odd number of times.For ExampleInput − digit = 12345 Output − count for even digits = 2 count for odd digits = 3Explanation − Yes, Also, even digits are occurring even number of times i.e. 2 and odd digits are occurring odd number ... Read More
We are given with the two strings let’s say str1 and str2 and the task is to find the count of common characters in two strings i.e. if str1[i] = str[j], then they will be considered as a pair and count will increased to 1 and if str1[i]!=str2[j] then they willn’t be considered as a pair and count willn’t increase to 1.For ExampleInput − str1 = “hello” str2 = “heoo” Output − count is: 3Explanation − str1[0] = str2[0] i.e. h ; str1[1] = str2[1] i.e. e ; str1[2]!=str2[2] i.e. l and o; str1[3]=str2[3] i.e. o. So, the ... Read More
We are given a string of any length and the task is to convert the string having uppercase letters to lowercase letters and lowercase letters to uppercase letters.For ExampleInput − string str = ”Welcome To The Site!”Output − wELCOME tO tHE sITE!Explanation − converted the letters W, T, T, S to lowercase and letters e, l, c, o, m, e, o, , i, t, e to uppercase and it doesn’t perform any operations to the special characters.Input − string str = ”HELLO”Output − helloExplanation − converted the letters H, E, L, L, E to lowercase.This can be done using two different approachesUsing inbuilt functions provided by ... Read More
The task is to convert the distance from kilometers to meters and centimeters in PL/SQL.PL/SQL is the extension of SQL which combines the Data manipulation of SQL with the working of procedural language.According to the problem we should have distance in kilometers whose value we have to convert in meters and centimeters.As per the conversion rule −1km = 1000 meters1km = 100000 centimetersAccording to this conversion rule we want the distance to be converted by a logic in PL/SQL.ExampleInput: kilometer = 10 Output: meter = 10000 Centimeter = 1000000 Input: kilometer = 9 Output: meter = 9000 Centimeter ... Read More
To display only unique records, use distinct() in MongoDB. Let us create a collection with documents −> db.demo613.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bd4f6b89257f5584d88") } > db.demo613.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bdbf6b89257f5584d89") } > db.demo613.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bddf6b89257f5584d8a") } > db.demo613.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be0f6b89257f5584d8b") } > db.demo613.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be2f6b89257f5584d8c") } > db.demo613.insertOne({"Name":"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be8f6b89257f5584d8d") }Display all documents from a collection with the help of find() method −> db.demo613.find();This will produce the following output −{ "_id" : ObjectId("5e988bd4f6b89257f5584d88"), "Name" : ... Read More
Check for a specific field using MongoDB $exists. If that field does not exist in a document, then you need to display the same document with find().Let us create a collection with documents −> db.demo612.insertOne({id:1, "Info":[{Name:"Chris", Age:21}, {Name:"David"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e987372f6b89257f5584d87") }Display all documents from a collection with the help of find() method −> db.demo612.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e987372f6b89257f5584d87"), "id" : 1, "Info" : [ { "Name" : "Chris", "Age" : 21 }, ... Read More
To sort documents in MongoDB 4, use sort(). To show only a single field, which is sorted, set it to 1.Let us create a collection with documents −> db.demo611.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo611.insertOne({"Name":"Adam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987115f6b89257f5584d84") } > db.demo611.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987118f6b89257f5584d85") } > db.demo611.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e98711bf6b89257f5584d86") }Display all documents from a collection with the help of find() method −> db.demo611.find(); This will produce the following output: { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : ... Read More
To add together a subset of elements of an array, use $first along with $sum. Let us create a collection with documents −> db.demo610.insertOne({Values:[10, 20, 30, 40, 50]});{ "acknowledged" : true, "insertedId" : ObjectId("5e9747b8f57d0dc0b182d62e") }Display all documents from a collection with the help of find() method −> db.demo610.find().pretty()This will produce the following output −{ "_id" : ObjectId("5e9747b8f57d0dc0b182d62e"), "Values" : [ 10, 20, 30, 40, 50 ] }Here is the query to add together a subset of elements of an array in ... Read More
To remove the entire array from the collection, use $unset in MongoDB. Let us create a collection with documents −> db.demo609.insertOne({"ListOfSubject":["MySQL", "MongoDB"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e974695f57d0dc0b182d62c") } > db.demo609.insertOne({"ListOfSubject":["Java"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e97469af57d0dc0b182d62d") }Display all documents from a collection with the help of find() method −> db.demo609.find();This will produce the following output −{ "_id" : ObjectId("5e974695f57d0dc0b182d62c"), "ListOfSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5e97469af57d0dc0b182d62d"), "ListOfSubject" : [ "Java" ] }Here is the query to remove the entire array from collection −> db.demo609.update({}, {$unset:{"ListOfSubject":""}}, {multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, ... Read More
To get unique values within two arrays in a document, use a $setUnion in aggregate(). The $setUnion takes two or more arrays and returns an array containing the elements that appear in any input array.Let us create a collection with documents −>db.demo608.insertOne({"ListOfName1":["John", "Chris", "Bob", "David"], "ListOfName2":["Bob", "Sam", "John", "Robert", "Chris"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e974542f57d0dc0b182d62b") }Display all documents from a collection with the help of find() method −> db.demo608.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e974542f57d0dc0b182d62b"), "ListOfName1" : [ "John", "Chris", ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP