## Java Program to Find the First Non-repeated Character in a String

Write a java program to find the first non-repeated character in a string.Given an input string, we have to write a java code to find the first non-repeated character in a string.

For example –

i) Input string – java
Output – j (j is the first non-repeating character in a string)

ii) Input string – web rewrite
Output – b (b is the first non-repeating character in a string)

## Check whether Two Strings are Anagrams of each other

Write a program to check whether two strings are anagrams of each other.

## What is an Anagram ?

Two strings are said to be anagrams of each other if it contains the same characters, only the order of characters in both the strings is different. In other words, both strings must contain the same exact letters in the same exact frequency.

## How to Check if a Number is a Power of 2

How to check if a number is a power of 2.  To understand this question, let’s take some example.

Example –

Input – 16   – 16 is a power of 2 (2^4).

Input – 15 – 15 is not a power of 2.

Input – 32- 32 is a power of 2 (2^5).

We can use multiple approaches to check whether a number is a power of 2 or not.

## Reverse a String in PHP without using strrev() Method

Write a script to reverse a string in PHP without using strrev() method. In PHP, we can reverse a string easily using strrev() method. But think how will you reverse a string without using an inbuilt strrev() method.

## Find Maximum Subarray Sum in an Array – Kadane’s algorithm

Write a program to find maximum subarray sum in an array. Given an array of N elements, find the maximum possible sum of a contiguous subarray. An array can contain both positive and negative values.

## Recursion vs Iteration – Difference between Recursion and Iteration

Recursion vs Iteration. What’s the difference between recursion and iteration. Recursion and Iteration both are two different programming approaches. In some cases recursion is best suited and in some other cases iterative way of programming is good.

In programming, repeated set of instructions can be handled either by using recursive or iterative approach in your code. So which approach you choose and why. Let’s talk about recursion vs iteration.

## Find Common Elements in Two Arrays – Intersection of two Arrays

Given two sorted arrays, Write a code to find common elements in two arrays. In other words, we have to write a code to find the intersection of two arrays.

Let’s assume following two sorted arrays arr1[] and arr2[].

int arr1[] = {2,  3,  4,  5,  6};
int arr2[] = {4,  6,  7,  8,  9};

So the common elements in these two arrays is 4 and 6.

## Generate Prime Numbers between 1 to N – Sieve of Eratosthenes

Write an efficient program to generate prime numbers between 1 to N (Where N is 10, 100, 1000 etc). This question can also be asked like this, Generate prime numbers between 1 to 100 or 1 to 10 etc.

Suppose the value of N is 10, So the prime numbers between 1 to 10 is 2, 3, 5, 7.  Now let’s learn the most efficient algorithm (Sieve of Eratosthenes) to generate Prime Numbers between 1 to N. Before solving this program, let’s understand what is a prime number.

## Find the Second Largest Number in an Unsorted Array

Write a Program to find the second largest number in an unsorted array. Given an array of unsorted values find the second largest number in an array.

For example – Let’s take an array.

arr[] = { 4, 1, 5, 2, 8, 0, 3, 55}

The second largest element in this array is 8.  So how do you find the second largest number in an array ?  There are multiple ways by which you can solve this problem.

Find Second Largest Number in an Array

First Approach : Sort an Array

The simple approach is  to sort an array using sorting algorithm. After sorting an array element at n-2 index will be the second largest element.

You can use following sorting algorithms.

iii) Bubble Sort

iv) Merge Sort

Selection sort, insertion sort and bubble sort are not suitable for large data sets as their time complexity is O(n2) . You can use Merge Sort or Quick Sort for large data sets.

NOTE –  If  values of an array is  repeated then this approach won’t work. Then the best approach is to traverse an array to find the second largest element.

Second Approach : Traverse an array

The best approach is to traverse an array to find the second largest number in an array. Using this approach maintain two flags one for highest and other for the second highest number. The time complexity of this approach is O(n).