## Java - To find the Second Highest Digit

find second largest number in array java

find second largest number in array java using scanner

java pgm to find the second largest element in an array

find largest number in array in java

find second largest number without using array in java

find third largest number in array without sorting in java

find second largest number in array javascript

I need to:

Write a function that accepts a string and returns the second highest numerical digit in the input as an integer.

The following rules should apply:

Inputs with no numerical digits should return -1

Inputs with only one numerical digit should return -1

Non-numeric characters should be ignored

Each numerical input should be treat individually, meaning in the event of a joint highest digit then the second highest digit will also be the highest digit

For example:

- "abc:1231234" returns 3
- "123123" returns 3

This is my code currently:

public class Solution { public static int secondHighestDigit(String input) { try{ int k = Integer.parseInt(input); char[] array = input.toCharArray(); int big = Integer.MIN_VALUE; int secondBig = Integer.MIN_VALUE; for(int i = 0; i < array.length; i++){ System.out.println(array[i]); for(int n = 0; n < array[i]; n++){ if(array[i] > big) { secondBig = big; big = array[i]; }else if(array[i] > secondBig && array[i] != big){ secondBig = array[i]; } } } System.out.println(secondBig); }catch(Exception e) { System.out.println("-1"); } return -1; } }

Tests:

import org.junit.*; import static org.junit.Assert.*; public class Tests { @Test public void test1() { Solution solution = new Solution(); assertEquals(3, solution.secondHighestDigit("abc:1231234")); } @Test public void test2() { Solution solution = new Solution(); assertEquals(3, solution.secondHighestDigit("123123")); } }

The program should print 3 for abc:1231234 and 123123 but instead it is returning -1 for both.

I am lost are where to go from here. I would be grateful if someone could help. Thanks.

One possible solution is to remove all non numeric characters and sort the string

public int secondGreatest(String s) { String newStr = s.replaceAll("[^0-9]*", ""); if (newStr.isEmpty() || newStr.length() == 1) { return -1; } else { char[] c = newStr.toCharArray(); Arrays.sort(c); return c[newStr.length() - 2] - '0'; } }

**Java Program to find Second Largest Number in an Array,** We can find the second largest number in an array in java by sorting the array and returning the 2nd largest number. Let's see the full example to find the second� Java Program to find Second Largest Number in an Array with examples of fibonacci series, armstrong number, prime number, palindrome number, factorial number, bubble sort, selection sort, insertion sort, swapping numbers etc.

Don't try to convert your string to an Integer at first - since you already know that it's possible it may contain some non-numeric characters.

Instead, parse through each character in the String, if it is an integer, add it to a list of integers. Let's use an ArrayList so we can add to the list and have it dynamically sized.

Your code should look something like this: (giving you some work to do)

// Iterate through characters in the string // You could also use "substring" so you don't have to deal with chars for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); if(Character.isDigit(c)) { // Convert c to an int appropriately and add it to your list of ints } } // Sort the list of ints in descending order // Now, we write a seperate method public int getNthBiggest(int n) { // Return the nth biggest item (you could just use 2) }

Checking if the string has 0 or 1 "digits" should be trivial enough for you, if you've gotten this far.

**WAP to find and print second largest digit in the given number ,** We do not do your HomeWork. HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to� Approach: Find the second largest element in a single traversal. Below is the complete algorithm for doing this: 1) Initialize two variables first and second to INT_MIN as first = second = INT_MIN 2) Start traversing the array, a) If the current element in array say arr[i] is greater than first.

Run this code:

private static int secondHighestDigit(String input) {

String pattern = "(\\d+)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(input); if (m.find( )) { String foundedDigits = m.group(0); System.out.println("Found value: " + m.group(0) ); char[] characters = foundedDigits.toCharArray(); List<Integer> integers = new ArrayList<>(); for(char c : characters){ integers.add(Integer.parseInt(c +"")); } if(integers.size()==1) return -1; Collections.sort(integers); System.out.println("One Biggest int: " + integers.get(integers.size()-1)); System.out.println("Two Biggest int: " + integers.get(integers.size()-2)); return integers.get(integers.size()-2); }else { System.out.println("NO MATCH"); return -1; } }

***[Code challenge]* find the second largest digit in a given number ,** find the second largest digit in a given number without using arrays and string operation. Given a number N.The task is to find the largest and the smallest digit of the number. Examples : Input : N = 2346 Output : 6 2 6 is the largest digit and 2 is samllest

**Find Second largest element in an array,** Find Second largest element in an array. 14-06-2017. Given an array of integers, our task is to write a program that efficiently finds the second largest element� And ifÂ digit is smallestÂ than value at variable smallestNumberÂ replace the value at smallestNumber with the new digit obtained; Next dividing the number by 10 so that after getting first digit it will be removed from original number. Loop will be continue upto number > 0.

**Find Second largest digit in given number without using any ,** using System; namespace ConsoleApplication1 { class Program{ static void Main (string[] args) {int num,k=0; int a = 0, b = 0, tmp = 0;num� C Program to find Second largest Number in an Array. This program for finding the second largest number in c array asks the user to enter the Array size, Array elements, and the Search item value. Next, this C program will find the Second largest Number in this Array using For Loop.

**Java program to find second largest number in an array,** Java program to find second largest number in an array. By candid | Posted : 18 Apr, 2016 | Updated : 2 Jul, 2017. Step 1: Iterate the given array. Step 2 (first if� if the number is 52163429 then highest and second highest are 9 & 6 . if the number is 45886 the highest and second highest should be 8 & 6.Also it should be done using loop and if statements – user2991964 Nov 14 '13 at 12:22

##### Comments

- "Where to go from here" is a very vague question. Please try to describe what issue you are facing with your program.
- The program should print 3 for abc:1231234 and 123123 but instead it is returning -1
- Maybe it helps to split the problem into smaller ones. We need to split the string into its parts. We need to detect if it is a number. We need to collect these in a list. Then sort the list. Then find the last and the second last item in that list. And then we would have very specific questions to ask for.
- Well that's kind of basic, you are only returning a hardcoded -1 from your method. You need to add another return statement earlier in the method.
- @JoakimDanielson Where should I put this Return statement and what should the value of this be? Thanks.
- Hi. I am getting an error when it trys "Arrays.sort(c);" It says Solution.java:9 error: cannot find symbol. Arrays.sort(c); EDIT: Just needed to import Java arrays.
- Would you be open to a DM?
- Sorry, but what's a DM ?
- Hi. It means Direct Message. I just wanted to know if you could explain to me what this line of code does?
`return c[newStr.length() - 2] - '0';`

I am just curious is all because I don't understand what the`-2] '0';`

means. Thanks. - I sent a message above but forgot to @ you.