Given an array of size N in which every number is between 1 and N, determine if there are any dupli

Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it.

 

 

 

 

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


public class Duplicate {

 
 public static boolean duplicate1(int[] number,int N){
  Set s=new HashSet();
  for(int i=0;i<N;i++)
  s.add(number[i]);
  if(s.size()<N)return true;
  else return false;
 } 
 
 
 public static boolean duplicate2(int[] number,int N){
  Arrays.sort(number);
  for(int i=0;i<N-1;i++)
  {
   if(number[i]==number[i+1])return true;
  }
  return false;
 }
 
 
 
 public static void main(String args[]){ 
  int N=3;
  int[] number=new int[N];
  for(int i=0;i<N;i++)
  {
   number[i]=(int)(Math.random()*N);  
      System.out.print(number[i]+" ");
  }
  
  System.out.println(duplicate1(number,N));
  
  System.out.println(duplicate2(number,N));
  
  
 }
}

 

你可能感兴趣的:(String,Class,ini,Duplicates)