Note that all words of length 1 return 0 from freq(). Perhaps int res = arr[0]; ... if(i == 0) { res += arr[i + 1]; ...?
freq()
int res = arr[0]; ... if(i == 0) { res += arr[i + 1]; ...