Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Wednesday, March 16, 2016

Remove Duplicate Elements from an Array.

Remove Duplicate Elements from Array              

a)     Using unique property of Sets which makes problem quite simple.
It is much better than second approach.

b)    Interviewers are more emphasized on second approach as it requires some tricky coding skills.

public class RemoveDuplicateElements {

static int array[] = new int[] { 10, 20, 30, 10, 15, 40, 30, 10, 20, 90, 50 };

     public static void main(String[] args) {
           // Using In-Build Collection API
           System.out.println("Using Approach 1");
           Approach1(array);

           // Using Code approach. Asked in Flipkart interview...
           System.out.println("Using Approach 2");
           Approach2(array);
     }

       // Prefer LinkedHashSet instead of HashSet so as to maintain order

public static void Approach1(int array[]) {
           Set<Integer> set = new LinkedHashSet<Integer>();

           for (int i = 0; i < array.length; i++)
           set.add(array[i]);
           Object[] objectArray = set.toArray();
          
     for (int i = 0; i < objectArray.length; i++)
           System.out.print(objectArray[i] + "\t");
           System.out.println();
     }

     // Replacing duplicate value with last value and decrement the size

     public static void Approach2(int[] array) {
           int sizeArray = array.length;
           for (int i = 0; i < sizeArray; i++) {
                for (int j = i + 1; j < sizeArray; j++) {
                     if (array[i] == array[j]) {
                           array[j] = array[sizeArray - 1];
                           sizeArray--;
                     }
                }
           }
           int newArray[] = Arrays.copyOf(array, sizeArray);
           for (int i = 0; i < newArray.length; i++)
                System.out.print(newArray[i] + "\t");
     }
}

Wednesday, February 3, 2016

To find second and first minimum element in an array

To find second and first minimum element in an array 

// Time Complexity : O(n) - Only 1 pass is required.
public class SecondMinimum {
      public static void main(String[] args) {

            int[] arr = new int[] { 34, 45, 21, 12, 54, 67, 15 };
            int first, second, arr_size = arr.length;

            /* There should be atleast two elements */
            if (arr_size < 2) {
                  System.out.println(" Invalid Input ");
                  return;
            }

            first = second = Integer.MAX_VALUE;
            for (int i = 0; i < arr_size; i++) {
                  /*If current element is smaller than first then update    both first and second*/
                  if (arr[i] < first) {
                        second = first;
                        first = arr[i];
                  }

                  /* If arr[i] is in between first and second then update second*/
                  else if (arr[i] < second && arr[i] != first)
                        second = arr[i];
            }
            if (second == Integer.MAX_VALUE)
                  System.out.println("There is no second" + "smallest element");
            else
                  System.out.println("The smallest element is " + first
                              + " and second Smallest" + " element is " + second);
      }
}

Wednesday, November 25, 2015

Difference Between Array, Arrays and ArrayList

Difference Between Array, Arrays and ArrayList



Array
Arrays
ArrayList
Package


package java.util;

package java.util;
List Interface
Defination
Array is part of core Java programming and has special syntax and semantics support in Java.


This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
ArrayList is part of Collection framework along with other popular classes e.g. Vector, Hashtable, HashMap or LinkedList.
Syntax

int arr[]=new int();
Arrays.sort(arr)
ArrayList<String> list=new ArrayList<String>();
Size
Array is  fixed length data structure.

Length of array cannot changed once it created.


ArrayList is a variable length Collection class.
Resizing operation in ArrayList is slow down performance as it creates new Array and copying content from old Array to new Array.

Generics
Array doesn’t support the Generics, as Array instance knows about what kind of type it can hold and throws ArrayStoreException.

ArrayList allows you to use Generics to ensure type-safety.
MultiDimential
Array can be multi-dimensional.

Array list is single dimensional.
Wrapper
Array allows to store both primitives and Objects in Java.

ArrayList doesn’t allow to store primitives in ArrayList, it can only contain Objects.