Many times developers encounter this scenario where they have an array of string values and another single string value and they may need to perform any of the following tasks : Check whether the string exists in the array or search array for the given string. If the string exists in the array, then get the index of its position in array. This post will detail out 5 different methods to search a string in an array to check if the array contains it or not with example programs. Method 1 : Iterating over the array This is a conventional and most used method whereRead More →

Shuffling the contents of an ArrayList is a problem which can test the logic of a person and also demonstrates knowledge of java api. This post contains a couple of methods which shuffle the contents of ArrayList. Method 1 : Without using Collections class This method utilizes the approach of random number generation which is within the size of the list and then swapping the elements at two different indices of the list. Algorithm Logic to shuffle ArrayList without using Collections class involves the following steps : Calculate the size of the list. Iterate over the list and in each iteration : a. Retrieve theRead More →

Problem Suppose there is a bunch of String objects and you want to join them by a separator to form a combined string. For Example, I have 3 strings “www”, “codippa” and “com”. I want to join these strings with a “.”(dot). Normal approach would be : String s1 = “www”; String s2 = “codippa”; String s3 = “com”; String joinedString = s1 + “.” + s2 + “.” + s3; This approach is fine but only when the number of strings to be joined is less. When the string values to be joined are more, this approach has following shortcomings: Code will be moreRead More →

Sorting means arranging elements in a specific order. Order can be of two types : ascending and descending. Ascending order is when the elements are arranged in order of increasing value(smaller to larger). Descending order implies the elements are arranged in order of decreasing value(or larger to smaller values). Sorting a one dimensional array in java is straight forward and easy, just call java.util.Arrays.sort with the array to sort as argument and you get the array sorted. But what if you want to sort a two dimensional array. There is no direct method to sort a two dimensional array in java. Just pause and think,Read More →

A fraction is a quantity which is not a whole number rather it is formed by using two numbers. A fraction has two parts : numerator and denominator separated by a “/”. Examples of fraction are : 15/2, 35/6 A fraction should always be represented in reduced(or simplest) form where there are no common multiples of numerator and denominator. Example, 56/12 is not in its reduced from since its numerator and denominator still have common multiples. It reduced form will be 14/3(dividing both numerator and denominator by 4). For reducing a fraction to its simplest form, Greatest Common Divisor(GCD) of its numerator and denominator isRead More →

Many times it happens when you are iterating over an ArrayList and you want to remove items from this list based on some condition. For Example, there is a list of students and you want to remove those students whose result is “Fail”. Code for this requirement is as simple as below :Read More →