java - How do I check if there are duplicate numbers in an array? -


this question has answer here:

i have find out if there duplicate numbers in array, , if there duplicate found need -5 points. here code far:

for (int k = 0; k < arrnums2.length; k++)  {     (int = 0; < arrnums2.length; i++)      {         if (arrnums2[k] == arrnumscompare[i])         {                          points = points - 5;                         (int j = 0; j < arrnums2.length; j++)             {                 if (arrnums2[k] == arrnums2[j])                 {                     arrnums2[j] = 0;                 }             }         }     } } 

i suggest 1 line solution :

integer[] numbers = {1,2,3,4,5,6,5,4,3,2,1};  return 5 * (new hashset<integer>(arrays.aslist(numbers)).size() - numbers.length); 

i add elements set, definition refuses duplicates. compare size of set length of array , multiply result 5.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -