Skip to content

Instantly share code, notes, and snippets.

Created May 4, 2013 12:52
Show Gist options
  • Save anonymous/5517423 to your computer and use it in GitHub Desktop.
Save anonymous/5517423 to your computer and use it in GitHub Desktop.
class fourtyseven2
{
public static void main (String[] args)
{
int[] data = {1000,1000,5,1000,4,12,-3,999,0,1000};
//check whether there are max and secondmax
boolean allthesame=true;
for(int value:data)
{
allthesame=(data[0]==value)? true:false;
if (allthesame==false)
{
break;
}
}
//declare & init
int max=data[0], secondmax=data[0];
if(allthesame==false)
{
//comp max
for(int value:data)
{
if(value>max)
{
max=value;
}
}
//change secondmax to value lower than max
for(int value:data)
{
if(value<max)
{
secondmax=value;
break;
}
}
//calc secondmax
for(int value:data)
{
if(value<max && value>secondmax)
{
secondmax=value;
}
}
}
//write out the two largest
System.out.println("max: "+max);
if(allthesame==false)
{
System.out.println("secondmax: "+secondmax);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment