Search This Blog

Showing posts with label for loop. Show all posts
Showing posts with label for loop. Show all posts

Wednesday, 9 April 2014

Find Minimum and Maximum value of Array in Java


Find Minimum and Maximum value of Array in Java


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

Sunday, 6 April 2014

Find Duplicates in an array

Find Duplicates in an array


package com.blogspot.rajvishwakarma15;

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

Java Program To Find Second Largest Number in Array

public class SecondLargestNumberInArray {
       public static void main(String[] args) {
              int arr[] = { 18, 23, 77, 81, 92, 88, 52, 48, 26, 66, 65, 76, 11, 85,
                           99, 53, 26, 61, 65, 34 };
              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);

       }
}


OutPut:

Second Largest in array is: 92