Afficher un message
Vieux 16/09/2007, 12h04   #1
Ruhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Search for duplicate elements in the array

Hello.
I have a pretty good working Java method, which sorts array of
strings, then searches for duplicate elements. If element is
duplicated output shows ">> hello *20 times" for example.

private void printArray()

{

String arr[] = str.split("\\n+");



Arrays.sort(arr);



for (int j = 0; j < arr.length; j++)

{

int count = 1;

for (int i = j + 1; i < arr.length; i++)

{

if (arr[i].compareTo(arr[j]) == 0)

{

count++;

j++;

}

}

if (count > 1)

{

System.out.println(">> " + arr[j] + " *" + count + " times");

} else

{

System.out.println(">> " + arr[j]);

}

}

}


But the problem is that I can't write this method in Ruby! How to make
this in Ruby?

  Réponse avec citation
 
Page generated in 0,05424 seconds with 9 queries