public static int m(int[]a){Arrays.sort(a);int i=0,j=0;for(int k:a)if(a[i]<a[j++])i++;return j-i;}
I originally came up with this, but, minified, it’s 102 characters:
public static int largestFrequency(int[] a) {
Arrays.sort(a);
int i = 0, j = 0;
for (; j < a.length; j++)
if (a[i] < a[j])
i++;
return j - i;
}
Then I realized that the loop header can be replaced with java’s enhanced for loop. It handles loop continuation checking automatically without having to explicitly write j < a.length, so it’s shorter. I don’t even access the variable k introduced by the enhanced for loop and I continue to increment j as if it’s still the old loop. The winning submission for week seven probably could have been shortened the same way.
public static int largestFrequency(int[] a) {
Arrays.sort(a);
int i = 0, j = 0;
for (int k : a)
if (a[i] < a[j++])
i++;
return j - i;
}
public static int z(int[]r){int m=0,c;for(int e:r){c=0;for(int i:r)m=e==i&&++c>m?c:m;}return m;}
96 characters
I dunno. I feel like I could do better. Lambda blah blah. Couldn’t work it in though.
uncompacted:
public static int ModeGolf(int[] integerArray) {
// by declaring currentCount here we save a couple of characters not having
// to declare it 'int' in the inner loop...
int maxCount=0, currentCount;
// originally planned to use a HashSet to cull elements but it wasn't worth it,
// when we loop through the elements, we end up counting duplicates, but the
// only consequence is repeating with a lower count, without hurting the result...
// plus it turned out to be a lot of characters, because you can only HashSet
// Objects not directly an array of 'int'. and likely convert them back to int
for (int arrayElement : integerArray) {
currentCount=0;
for (int arrayElementInner : integerArray) {
// using a ternary saved a few extra characters over the corresponding
// 'if' statement. so did moving the assignment outside the ternary, as
// opposed to doing the assignment in the result, which you can't do in
// java, anyway, apparently.
maxCount =
// phew, order of operations didn't require any parens here.
((arrayElement == arrayElementInner) && (++currentCount > maxCount))
? currentCount
: maxCount;
}
}
// QED
return maxCount;
}
agreed, @jus1in, that’s epic i twiddled around for far too long on my latest and couldn’t drop a single byte… i intend to actually try to do something productive rather than golf today. xD i like that yours is actually using a few clever concepts instead of just a lot of individual little optimizations…
For some next context I’d be really interested in something like:
A method that takes two Strings and returns the smallest possible number of changes you need to turn the first String into the second. Possible changes would be:
Adding a char
Changing a char
Removing a char
So compare("chllrnge", "challenge") would return 2 (Add an ‘a’ and change ‘r’ to ‘e’)
or compare("muimecraf", "minecraft") would return 3 (Remove an ‘u’ and change ‘m’ to ‘n’ and add an ‘t’)
What do you think? Could this be a good challenge?
public static int m(int[]a){int i=1;for(String m=".*( .+ ).*\\1.*";Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ").matches(m);m+="\\1.*")i++;return i;}
153 characters
I tried using regex to solve the challenge. This solution will work for any printable array as long as the Object’s toString() doesn’t contain any ‘[’, ‘]’ or ’ ’
public static int mode(int[] a){
int i = 1;
String s = Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ");
for(String m=".*( .+ ).*\\1.*";s.matches(m);m+="\\1.*")i++;
return i;
}
Here’s the one for String[]:
public static int mode(String[] a){
int i = 1;
String s = Arrays.toString(a).replaceAll("[\\[\\]\\, ]"," ");
for(String m=".*( .+ ).*\\1.*";s.matches(m);m+="\\1.*")i++;
return i;
}
It works by making a string with a space before and after each number then checks how many backreferences there are to the regex “( .+ )”