Sunday, 6 February 2022

Error: More than one module matches. Use skip-import option to skip importing the component into the closest module

 When I try to create a component in the angular cli, it's showing me this error. How do I get rid of it ?

Specify the module using the --module parameter. For example, if the main module is app.module.ts, run this:

ng g c new-component --module app

Or if you are in an other directory then

ng g c component-name --module ../

unable to connect to web server iisexpress

 


When I try to start any .NET Core Web Application I get "unable to connect to web server iisexpress". If If I change the profile to start the "project", it starts up fine.

I have tried the following:
1) Uninstall IIS Express 10. Reinstall through the Visual Studio 2017 setup.
2) Delete the IISExpress folder under My Documents. Delete the ApplicationConfig under the .vs folder.
3) Changing the port number

I am using Visual Studio 2017 15.5.5 on Windows 7 Pro SP1. It's running dotnet core 2.1.4.

If in your launchSettings.json file is not available in your project, then please add it and Copy and Paste below code.

Change Port number according to your application.


Here is the launchSettings:

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:2732/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Sample": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:2733/"
}
}
}

Thursday, 3 February 2022

Find missing number between 1 to N in array in C#

 Find missing number in the sequence in c#- Learn how to find the missing number among the sequence of numbers from 1 to n with example Programs.

For Example following are the numbers from 1 to 6 as 1,2,3,5,6.
The missing number in the above sequence is 4.

Input

1,2,3,5,6

Output

Missing Number : 4

How to calculate missing number?

Solution:
total numbers=5
sum of 6 numbers from 1 to 6 =6*(6+1)/2;
=21;

sum of the Numbers in the sequence =17.

missing Number=sum of 6 numbers-sum of the Numbers in the sequence
= 21-17 =4

Method 1:(Using Mathematical formula)

Algorithm:

step1: calculate sum of Numbers : n*(n+1)/2 – say as x
step2: calculate sum of numbers in the sequence – say as y
step3: subtract x-y to get the missing Number.

C# Programming Example

using System;
class Find_Missing_Number
    {
        static void Main(string[] args)
        {
            //array to find the missing number between 1 and 10
            // Simplicity, We will take number 1 to 10 i where Number 5 is missing in the sequence.
            int[] arr = { 1, 2, 3, 4, 6, 7, 8, 9, 10 };

            int missingNumber,Totalsum;
            // Accoreding to series rule, calculating sum of total numbers upto 10
            //sum of first n natutal numbers=n*(n+1)/2
            Totalsum = (arr.Length + 1) * (arr.Length + 2) / 2;

            // Missing number is calculating.
            foreach (int item in arr)
            {
                Totalsum = Totalsum - item;
            }
            missingNumber = Totalsum;

            Console.WriteLine("missing number  : {0}",missingNumber);
        }
    }

Output
missing number : 5

Method 2:

Algorithm:
step1: XOR all the array elements, let the result of XOR be X1.
step2: XOR all numbers from 1 to n, let XOR be X2.
step3: XOR of X1 and X2 gives the missing number.

C# Programming Example

using System;
 class Find_Missing_Number
    {
        static void Main(string[] args)
        {
            //array to find the missing number between 1 and 10
            // Simplicity, We will take number 1 to 10 i where Number 5 is missing in the sequence.
            int[] arr = { 1, 2, 3, 4, 6, 7, 8, 9, 10 };

            int missingNumber;
            int x1or = 0;
            int x2or = 0;
            // Accoreding to series rule, calculating sum of total numbers upto 10
            //sum of first n natutal numbers=n*(n+1)/2
           

            // Missing number is calculating.
            //Perform xor on all the elements of the array
            foreach (int item in arr)
            {
                x1or = x1or ^ item;
            }
            //perforn xor on 1 to n+1
            for (int i = 1; i <= arr.Length+1; i++)
            {
                x2or = x2or ^ i;

            }

            missingNumber=(x1or^x2or);

            Console.WriteLine("missing number  : {0}", missingNumber);
        }
        
    }

Output
missing number : 5

C# program to merge two sorted arrays into one

In this article, we will write a C# program to merge two sorted arrays into one

 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodingAlgorithms
{
    //You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B.  Write a method to merge B into A in sorted order.
    //Source: Cracking the Coding Interview 5th Edition p. 121
    public static class MergeSortedArrays
    {
        //x array is assumed to be the length of x + y, and lastX is the position of the last stored element in x array
        public static int[] MergeArrays(int[] x, int[] y, int lastX)
        {
            int xIndex = lastX;
            int yIndex = y.Length - 1;
            int mergeIndex = x.Length - 1;
            while (yIndex >= 0)
            {
                if (y[yIndex] > x[xIndex])
                {
                    x[mergeIndex] = y[yIndex];
                    yIndex--;
                }
                else if (y[yIndex] < x[xIndex])
                {
                    x[mergeIndex] = x[xIndex];
                    xIndex--;
                }
                mergeIndex--;
            }
            return x;
        }
    }
}

Convert String to Character Array in C#

 

In this article, we will write a C# program to convert String to Character Array in C#

 

   class Program
    {
       static void Main(string[] args)
        {
            string value = "Welcome To Csharpstar";
            // Use ToCharArray to convert string to array.
            char[] array = value.ToCharArray();
            // Loop through array.
            for (int i = 0; i < array.Length; i++)
            {
                // Get character from array.
                char ch = array[i];
                // Display each character.
                Console.Write("Character: ");
                Console.WriteLine(ch);
                Console.ReadLine();
            }
        }
    }

 

Output:

Character: W
Character: e
Character: l
Character: c
Character: o
Character: m
Character: e
Character:
Character: T
Character: o
Character:
Character: C
Character: s
Character: h
Character: a
Character: r
Character: p
Character: s
Character: t
Character: a
Character: r

C# program to Determine if Two Words Are Anagrams of Each Other

 

In this article, we will learn if two strings are anagram to each other.

Two words are said to be Anagrams of each other if they share the same set of letters to form the respective words.for an example: Silent–>Listen, post–>opts.

This is a frequently asked interview question.

namespace Anagram  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
           //Receive Words from User  
            Console.Write("Enter first word:");  
            string word1 = Console.ReadLine();  
            Console.Write("Enter second word:");  
            string word2 = Console.ReadLine();  
   
            //Add optional validation of input words if needed.  
            //.....  
   
            //step 1  
            char[] char1 = word1.ToLower().ToCharArray();  
            char[] char2 = word2.ToLower().ToCharArray();  
   
            //Step 2  
            Array.Sort(char1);  
            Array.Sort(char2);  
   
            //Step 3  
            string NewWord1 = new string(char1);  
            string NewWord2 = new string(char2);  
   
            //Step 4  
            //ToLower allows to compare the words in same case, in this case, lower case.  
            //ToUpper will also do exact same thing in this context  
            if (NewWord1 == NewWord2)  
            {  
                Console.WriteLine("Yes! Words \"{0}\" and \"{1}\" are Anagrams", word1, word2);  
            }  
            else  
            {  
                Console.WriteLine("No! Words \"{0}\" and \"{1}\" are not Anagrams", word1, word2);  
            }  
   
            //Hold Console screen alive to view the results.  
            Console.ReadLine();  
        }  
    }  
}  

The logic is:
1. Convert both strings to character arrays.
2. Sort the character arrays in ascending/descending order, but use the same ordering on both of the character sets.
3. Create two strings out of the two sorted character set arrays.
4. Compare the strings.
5. If they are not equal, they are not Anagrams.

C# program to determine if any two integers in array sum to given integer

 

In this article, we will learn how to determine if two integers in array sum to given integer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodingAlgorithm
{
    //Given an integer and an array of integers determine whether any two integers in the array sum to that integer.
    public static class TargetSum
    {
        //Brute force solution, O(n^2) time complexity
        public static bool TwoIntegersSumToTarget(int[] arr, int target)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                for (int k = 0; k < arr.Length; k++)
                {
                    if (i != k)
                    {
                        int sum = arr[i] + arr[k];
                        if (sum == target)
                            return true;
                    }
                }
            }
            return false;
        }
    }
}