PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Search for duplicate elements in the array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Search for duplicate elements in the array

Réponse
 
LinkBack Outils de la discussion
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
Vieux 16/09/2007, 12h32   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search for duplicate elements in the array

On 16.09.2007 13:04, Ruhe wrote:
> 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?


This is way inefficient. You rather want a counter per element stored
in a Map (HashMap or TreeMap in Java).

dups = Hash.new 0
arr.each {|x| dups[x]+=1}
puts dups.select {|k,v| v>1}

You can have it on one line if you prefer:

puts arr.inject(Hash.new(0)) {|d,x| d[x]+=1; d}.select {|k,v| v>1}

Kind regards

robert
  Réponse avec citation
Vieux 16/09/2007, 13h13   #3
Ruhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search for duplicate elements in the array

On 16 , 15:32, Robert Klemme <shortcut...@googlemail.com> wrote:
> This is way inefficient. You rather want a counter per element stored
> in a Map (HashMap or TreeMap in Java).
>
> dups = Hash.new 0
> arr.each {|x| dups[x]+=1}
> puts dups.select {|k,v| v>1}
>
> You can have it on one line if you prefer:
>
> puts arr.inject(Hash.new(0)) {|d,x| d[x]+=1; d}.select {|k,v| v>1}
>
> Kind regards
>
> robert


Thanks a lot! This is a new level of programing knowledge for me.
I'll try to make something like this with HashMap in Java.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h17.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08224 seconds with 11 queries