Update:
The question has been changed significantly. Formerly, when answering this, the input file should look like:
apple apple banana orange apple orange
banana orange apple
...
However, the solution will work anyway, but might be a little bit too complicated for this special use case.
The following awk script will do the job:
awk '{i=1;while(i <= NF){a[$(i++)]++}}END{for(i in a){if(a[i]>1){print i,a[i]}}}' your.file
Output:
apple 3
orange 2
It is more understandable in a form like this:
#!/usr/bin/awk
{
i=1;
# iterate through every field
while(i <= NF) {
a[$(i++)]++; # count occurrences of every field
}
}
# after all input lines have been read ...
END {
for(i in a) {
# ... print those fields which occurred more than 1 time
if(a[i] > 1) {
print i,a[i];
}
}
}
Then make the file executable and execute it passing the input file name to it:
chmod +x script.awk
./script.awk your.file