Ten problems need to be solved as a beginner

Fundamental problem solving at the beginning of the programming journey

1) Find the prime numbers from an array:

You have an array of numbers = [2, 3, 6, 7, 13, 5, 139, 11, 16], and you have to find prime numbers from the collection and return an array as an output.

First, find what makes a number prime number. If a number is divided by 1 and only with this number itself, then that’s called prime. So, this should be the primary condition in finding the prime number.

Second, we have to put prime numbers to an array and finally return this array as an output. That solves our problem.

2) Find the largest element of an array

You have an array of numbers = [2, 3, 6, 7, 70, 13] and you have to find the largest number.

First, we make a variable named ‘largeNum’ for storing value and initially we assign the value to 0. Then, we loop through this array.

Second, during looping each time, if we find a value greater than ‘largerNum’ we assign the large value to this. By the end of looping this array we get the largest number.

3) Reverse a string

You have string ‘Hello World’. Now you have to reverse the string like this ‘dlroW olleH’

We have to go through each element from last to first and concatenate element in the new string first to last or vice versa.

4) Remove duplicate from an array

You have an array [1, 1, 5, 5, 3, 5, 56, 39, 8] and you need to remove duplicate item from it.

First, we declare an empty array to store unique item. Then we loop through the original array and check whether the element is in the unique array or not. If the element is not in the unique array, then we push the element into the array.

5) Find the factorial of a number (without Recursion)

Any factorial of number is the series of multiplication of the integer number from 1 to the number itself. I mean 5! = 5*4*3*2*1 = 120. So, we have to multiply a number from 1 to the number itself and for this purpose we have loop.

Factorial for 1 or 0 is 1 and for negative number is complex number which we don’t bring under consideration.

6) Find factorial of a number (with Recursion)

In this problem, we have to find the same thing but in a recursive way. What recursion means? When a function calls itself it’s called recursive. For solving the problem in a recursive way, we have to make the way to get out of the loop of recursive calling function again and again. When number is less zero we always return 1 and that’s going to be out stopping condition to stop the recursive calling.

7) Find the nth element in Fibonacci sequence (with Recursion)

Fibonacci sequence starts with the 0 and 1 and the next element is going to be the sum of previous to number. In this manner Fibonacci sequence is made. Now we have to find nth position number in this series where 0 and 1 are the first and second element. Here, we use recursion for simplicity of code. But we can get the same result with techniques.

8) Find the array of Fibonacci series up to nth position (without Recursion)

Here, I have to put the same condition for finding nth element and that is the sum of previous to number.

First, we define the first element in the series and then for the next element we add the previous two element.

9) Find the array of Fibonacci series up to nth position (with Recursion)

In finding array of Fibonacci with Recursion is very interesting because here we apply the same principle but in addition we add the recursion call. Then, we push the element to the series.

10) Find the sum of number in an array with for loop, forEach and reducer

Finding sum of the element in an array is easy. Just loop through an array with for loop or forEach or reducer according their own mechanism and finally add the element into the resultant value.

forEach() method takes element as a parameter and return the addtion expression with the resultant sum variable which you add with every iteration.

reduce() method takes two parameter one is callback function and another is initial value. In the callback function we can pass two one is accumulator and another is iterative element value. Finally in the return statement we add the current value with accumulated value which gives the total sum of an array.

All of them gives the same result.

Web Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store