I am taking an introductory course in java in high school, however I have some experience in python. I was tasked to find the number of times a character occurs in a string so I did this.
String a = "hello"; String letter = "l"; int occurrence = a.length() - a.replace(letter, "").length();
Happy that I came up with a clever one-liner, I showed it to my teacher and she said that it's more elegant to use a
for loop. What does she mean by that? How do we define elegance in code? Is it subjective? As an aside is this more efficient than a for loop or vice versa?