How do you remove duplicates from a String?

This question is about to delete or remove duplicate characters from a given String in Java. You have to write an algorithm or code in Java.

have a look at the link Remove duplicates from a String

How to find start and end index of a given element in a sorted array?

This question is regarding to find out the start and end index of the given element in a sorted array. So it means the array contains duplicate elements.

Let’s say a sorted array is given with the elements in it as [1 2 2 3 3 3 3 5 6] and you need to find out the start and end indices of element 3. Therefore the start index is 3 and end index is 6.

Now you can iterate the array and find out the indices but it won’t give you the result in optimized way. So to find out the result in optimized way you need to use Binary Search algorithm and you can find the solution here using Kotlin. The same algorithm you can implement in other language, for example, in Java given below:

public class StartAndEndIndexOfValueInArray {
	public static void main(String[] args) {
		int[] arr = new int[] { 1, 1, 2, 2, 3, 3, 3, 3, 3, 5, 8, 6, 9 };
		StartAndEndIndexOfValueInArray findIdx = new StartAndEndIndexOfValueInArray();
		int totalElements = arr.length;
		int startIndex = findIdx.findStartIndex(arr, 0, totalElements, 3);
		int endIndex = findIdx.findEndIndex(arr, 0, totalElements, 3, totalElements);
		System.out.println("Start Index: " + startIndex + ", End Index: " + endIndex);
	}
		   
	private int findStartIndex(int[] arr, int low, int high, int x) {
		if (high > low) {
			int mid = low + (high - low) / 2;
			if ((mid == 0 || x > arr[mid - 1]) && x == arr[mid]) {
				return mid;
			} else if (x > arr[mid]) {
				return findStartIndex(arr, mid + 1, high, x);
			} else {
				return findStartIndex(arr, low, mid - 1, x);
			}
		}
		return -1;
	}
		   
	private int findEndIndex(int[] arr, int low, int high, int x, int totalElements) {
		if (high > low) {
			int mid = low + (high - low) / 2;
			if ((mid == totalElements - 1 || x < arr[mid + 1]) && x == arr[mid]) {
				return mid;
			} else if (x < arr[mid]) {
				return findEndIndex(arr, low, mid - 1, x, totalElements);
			} else {
				return findEndIndex(arr, mid + 1, high, x, totalElements);
			}
		}
		return -1;
	}
}

Write an algorithm to return an output array of products of all other numbers from a given array.

Let’s say you are given an array of non-zero integers. So you have to return an output array of products of all other numbers except the number itself.

For example, an input array a[]={1, 2, 3, 4}, so the output array will be out[]={24, 12, 8, 6}.

Please find the solution here

Write an algorithm to find out reverse of a given String.

We have different ways to reverse a string in Java as given below:

Using reverse() method in StringBuffer
Using reverse() method in StringBuilder
Using custom reverse method
Using recursion

You can find the details here at  String reverse example in java

Find out minimum moves to equalize array elements.

This is about to find out minimum moves to equalize two array elements at each index.

You can find the solution here at Minimum moves to equal array elements

How would you check if a binary tree is binary search tree?

A binary search tree (BST) is a node based binary tree data structure which satisfies the following properties:

The left sub-tree of a node contains only nodes with keys less than the root node’s key.

The right sub-tree of a node contains only nodes with keys greater than the root node’s key.

Both the left and right sub-trees must also be again binary search trees.

Please find the implementation to check if a binary tree is binary search tree.

Find out minimum moves to segregate even followed by odd elements in an array.

In this solution you have to arrange even numbers first, and then odd numbers and finally count the number of moves required to arrange all elements in the desired order.

You can find the solution here

How to read last n lines from the file?

Here is the solution to read n last lines from a file using Java.

How to segregate positive followed by negative array elements?

Please refer to the example Segregate positive followed by negative elements in Array