Find the duplicates in the following array of Strings. (Note in the text, there is a mistake in the code. The variable words is not properly declared as a String array. See: error page).
String[] words = {"I", "love", "coffee", "I", "love", "tea" };  
for (int i = 0; i < _______________; i++)  {   
  for (int j = __; j <  _______________; j++)  {   
    if (___________________)  {   
      println(____________  + " is a duplicate.");  
    }   
  }   
}
Answer:
String[] words = {"I", "love", "coffee", "I", "love", "tea" };  
for (int i = 0; i < words.length; i++)  {   
  for (int j = i+1; j <  words.length; j++)  {   
    if (words[i].equals(words[j]))  {   
      println(words[i] + " is a duplicate.");  
    }   
  }   
}   


»

No comments yet.

Leave a comment