Java Array, Finding Duplicates

前端 未结 14 1120
闹比i
闹比i 2020-11-22 09:13

I have an array, and am looking for duplicates.

duplicates = false;
for(j = 0; j < zipcodeList.length; j++){
    for(k = 0; k < zipcodeList.length; k++         


        
14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 10:03

    This program will print all duplicates value from array.

    public static void main(String[] args) {

        int[] array = new int[] { -1, 3, 4, 4,4,3, 9,-1, 5,5,5, 5 };
        
        Arrays.sort(array);
    
     boolean isMatched = false;
     int lstMatch =-1;
          for(int i = 0; i < array.length; i++) {  
              try {
                    if(array[i] == array[i+1]) { 
                        isMatched = true;
                        lstMatch = array[i+1]; 
                    }
                    else if(isMatched) {
                        System.out.println(lstMatch);
                        isMatched = false;
                        lstMatch = -1;
                    }
              }catch(Exception ex) {
                  //TODO NA
              }
    
          }
          if(isMatched) {
              System.out.println(lstMatch);
          }
    
    }
    

    }

提交回复
热议问题