Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Saturday, 3 March 2012

java program to find second largest number in array


package com.usr.arrays;

public class SecondLargestNumberInArray {
       public static void main(String[] args) {
              int arr[] = { 1, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              secondLargeNumber(arr);
       }

       public static void secondLargeNumber(int[] arr) {
              int largest = arr[0];
              int secondLargest = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (arr[i] > largest) {
                           secondLargest = largest;
                           largest = arr[i];
                         
                     } else if (arr[i] > secondLargest) {
                           secondLargest = arr[i];
                         
                     }
              }
              System.out.println("second largest in array is:" + secondLargest);

       }
}

Tuesday, 21 February 2012

Find Duplicates in an array


package com.usr.arrays;

public class CheckDuplictesInArray {
       public static void main(String[] args) {
              String[] strArr = new String[] { "a", "b", "c", "s", "d", "e", "a",
                           "b", "s" };
              detectDuplicates(strArr);
       }

       public static void detectDuplicates(String[] arr) {

              for (int i = 0; i < arr.length; i++) {
                     for (int j = i + 1; j < arr.length; j++) {
                           if (arr[j] == arr[i]) {
                                  System.out
                                                .println("Duplicate found! Original was at location "
                                                              + (i + 1) + " and match is at " + (j + 1));
                                  System.out.println("Duplicated value is:" + arr[j]);
                           }
                     }
              }

       }
}
OUTPUT
Duplicate found! Original was at location 1 and match is at 7
Duplicated value is:a
Duplicate found! Original was at location 2 and match is at 8
Duplicated value is:b
Duplicate found! Original was at location 4 and match is at 9
Duplicated value is:s

Monday, 20 February 2012

Find Min and Max value of Array


package com.usr.arrays;

public class FindMinAndMaxValueInArray {
       public static void main(String[] args) {
              int arr[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              findMaxValue(arr);
              findMinValue(arr);
       }

       public static void findMinValue(int[] arr) {
              int min = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (min > arr[i]) {
                           min = arr[i];
                     }
              }
              System.out.println("minimum value of the array is:" + min);
       }

       public static void findMaxValue(int[] arr) {
              int max = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (max < arr[i]) {
                           max = arr[i];
                     }
              }
              System.out.println("maximum value of array is:" + max);

       }
}
output
maximum value of array is:92
minimum value of the array is:23

Reverse Array Elements


package com.usr.arrays;

public class ReverseArray {
       public static void main(String[] args) {
              int[] intArr = new int[] { 1, 2, 3, 4, 5, 6, 7 };
              System.out.println("Array elements before reverse");
              displayArrayElements(intArr);
              reverseArrayElements(intArr);
              System.out.println("reversed array elements");
              displayArrayElements(intArr);
       }

       public static void displayArrayElements(int[] arr) {
              for (int temp : arr) {
                     System.out.println(temp);
              }
              System.out.println("..............................");
       }

       public static void reverseArrayElements(int[] arr) {
              int temp = 0;
              for (int start = 0, end = arr.length - 1; start < end; start++, end--) {
                     temp = arr[start];
                     arr[start] = arr[end];
                     arr[end] = temp;
              }

       }
}
output
Array elements before reverse
1
2
3
4
5
6
7
..............................
reversed array elements
7
6
5
4
3
2
1
..............................

java program to sort an array


package com.usr.arrays;

import java.util.Arrays;

public class SortingArrays {
       public static void main(String[] args) {
              String[] unsortStrArr = { "one", "two", "three", "four", "five", "six" };
              int[] unsortIntArr = new int[] { 9, 6, 4, 1, 4, 98 };
              System.out.println("String Array Elements Before sorting");
              displayStringArrayElements(unsortStrArr);

              System.out.println("int array elements Before sorting");
              displayIntArrayElements(unsortIntArr);

              // public static void sort(Object[] a)
              // Sorts the specified array of objects into ascending order, according
              // to the natural ordering of its elements.
              Arrays.sort(unsortStrArr);
              System.out.println("String array elements after sorting");
              displayStringArrayElements(unsortStrArr);

              Arrays.sort(unsortIntArr);
              System.out.println("int array elements after sorting");
              displayIntArrayElements(unsortIntArr);
       }

       public static void displayIntArrayElements(int[] intArr) {
              for (int temp : intArr) {
                     System.out.println(temp);
              }
              System.out.println(".............................");

       }

       public static void displayStringArrayElements(String[] strArr) {
              for (String temp : strArr) {
                     System.out.println(temp);
              }
              System.out.println("..........................");
       }
}
OUTPUT 
String Array Elements Before sorting
one
two
three
four
five
six
..........................
int array elements Before sorting
9
6
4
1
4
98
.............................
String array elements after sorting
five
four
one
six
three
two
..........................
int array elements after sorting
1
4
4
6
9
98
.............................