Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/01/2011, 07:41
Avatar de Adler
Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Contar palabras iguales

Hola

Trabaja con esto
Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>
  5. <script type="text/javascript">
  6. Array.prototype.EliminarRepetidos = function () {
  7. var arr_limpio = [];
  8. var val_eliminados = [];
  9. var cantidad = {};
  10.     for(var i = 0; i < this.length; i++){
  11.         if(!(this[i] in cantidad)) {
  12.             cantidad[this[i]] = 0;
  13.             arr_limpio.push(this[i]);
  14.             cantidad[this[i]]++;
  15.         } else {
  16.             val_eliminados.push(this[i]);
  17.             cantidad[this[i]]++;
  18.         }
  19.     }
  20.  
  21. var repeticiones = "";
  22.     for (j=0; j < arr_limpio.length; j++) {
  23.         repeticiones +="El elemento " + arr_limpio[j] + " se repite ------ " + parseInt(cantidad[arr_limpio[j]]-1) +" veces\n"
  24.     }
  25.     alert("Arreglo Original: " + array.toString() + "\nArreglo Limpio: " + arr_limpio.toString() + "\nValores Eliminados: " + val_eliminados.toString() + "\nCantidad de veces que se repite cada elemento\n=======================\n" + repeticiones)
  26. }
  27.  
  28.  
  29. function ContarRepetidos() {
  30. var array1 = [1,2,3,4,5,6,7,8,9,0];
  31. var array2 = [1,3,5,7,9];
  32. var valores = array1.concat(array2);
  33. for (i=0; i < valores.length; i++) {
  34. cadena = valores.join('');
  35. }
  36. array = cadena.split('');
  37. obj = array.EliminarRepetidos();
  38. }
  39.  
  40.  
  41. function ContarRepetidos2(valores) {
  42. var val = valores;
  43. array = val.split('');
  44. obj = array.EliminarRepetidos();
  45. }
  46. </script>
  47. </head>
  48. <body>
  49. <h1>Se Eliminan elementos repetidos.<br/>
  50. Se cuenta el número de veces que se repite cada elemento</h1>
  51. <input type="button" value="ver" onclick="ContarRepetidos();" /><br />
  52. <input type="button" value="ver" onclick="ContarRepetidos2('1283434536473846390');" />
  53. </body>
  54. </html>
Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />