Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/05/2013, 17:37
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Todos los elementos de un vector distintos

Puedes usar Set para hacer una comprobación rápida.

Código Java:
Ver original
  1. import java.util.Set;
  2. import java.util.HashSet;
  3.  
  4.  
  5. public class Utils {
  6.  
  7.     public static void main(String args[]) {
  8.         int[] m1 = {1, 2, 3, 4};
  9.         int[] m2 = {1, 2, 2, 1};
  10.  
  11.         System.out.println(todosDistintos(m1));
  12.         System.out.println(todosDistintos(m2));
  13.  
  14.     }
  15.  
  16.     public static boolean todosDistintos(int [] m) {
  17.         Set<Integer> s = new HashSet<Integer>();
  18.         for (int index = 0; index < m.length; index++)
  19.             s.add(m[index]);
  20.         return s.size() == m.length;
  21.     }
  22. }