Results 1 to 5 of 5
  1. #1
    anjijava16 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default how to find 2nd largest array if array values like{10,20,92,81,92,34}

    how to find 2nd largest array if array values like{10,20,92,81,92,34}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

    Where is your code? Do you have a question?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    anjijava16 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

    Quote Originally Posted by jim829 View Post
    Where is your code? Do you have a question?

    Regards,
    Jim
    package com.usr.arrays;

    public class SecondLargestNumberInArray {
    public static void main(String[] args) {
    int arr[] = { 1, 23, 92, 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];
    System.out.println(arr.length);
    System.out.println(largest);
    for (int i = 0; i < arr.length; i++) {
    if (arr[i] > largest) {
    secondLargest = largest;
    largest = arr[i];

    } else if (arr[i] > secondLargest&secondLargest!=largest) {
    secondLargest = arr[i];

    }
    /*if(arr[i]<largest){
    secondLargest = largest;
    largest = arr[i];

    } else if (arr[i] > secondLargest) {
    secondLargest = arr[i];

    }*///For 1st Largest Number

    }
    System.out.println("second largest in array is:" + secondLargest);

    }
    }
    ==============================
    In This case my array contain 2 values are same

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

    Try approaching it from the perspective of already knowing the largest value.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to find the number of unique values in an Array
    By djbeko06 in forum New To Java
    Replies: 7
    Last Post: 09-12-2011, 09:45 AM
  2. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 02:47 PM
  3. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 05:08 PM
  4. second largest array
    By bishnu in forum New To Java
    Replies: 2
    Last Post: 01-03-2009, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •