- 給定一個數組,判斷該數組中是否含有。若有,輸出該數組中所有重復的元素,返回true;若無,返回false。
- 示例,如下數組numbers,輸出[2,3,5],返回true
int[] numbers = {1,3,4,5,5,3,2,3,2,2,2};
1.思路
現將數組numbers按照從小到大進行排序(從大到小亦可),然后依次比較相鄰的兩個元素,若兩元素相同,則為重復元素,將其存入數組duplication中。
需要注意的是,如果存在多個(大於3個)相同的相鄰元素時,duplication數組也會出現重復的元素,如有四個相同的相鄰元素2{2,2,2,2},按照前面的做法,duplication數組中會有三個2(因為連續三次比較結果都相同),不滿足題目需求。
解決此bug的方法有兩個(目前博主只能想到這兩個):
1)根據連續比較次數,判斷是否應該將當前重復元素存入duplication數組。思路如下:
int 比較次數times = 0;
for(.......遍歷排序后的數組numbers.....){
if(當前數組元素 == 下一個數組元素)
比較次數times ++;
if(如果比較次數times == 1)
當前重復元素存入duplication數組;
else
比較次數times = 0;
}
2)利用集合Set的特性——元素唯一性。簡單介紹思路如下
定義一個集合對象set,遍歷遍歷排序后的數組numbers時,在set中添加 滿足if(當前數組元素 == 下一個數組元素 && set中不包含當前數組元素)的元素,
2.供參考代碼
1)思路一實現
1 import java.util.*; 2 3 public class TEST{ 4 public static void main(String[] args) { 5 TEST t = new TEST(); 6 int[] numbers = {1,3,4,5,5,3,2,3,2,2,2}; 7 int[] dup = new int[numbers.length/2+1]; 8 9 System.out.println("原numbers數組:"); 10 for (int i = 0; i < numbers.length; i++) { 11 System.out.println("numbers["+i+"]:"+numbers[i]); 12 } 13 14 if(t.duplicate(numbers,numbers.length,dup)) 15 { 16 System.out.println("含有重復元素dup:"); 17 for (int i = 0; i < dup.length; i++) { 18 System.out.println("dup["+i+"]:"+dup[i]); 19 } 20 } 21 22 } 23 public boolean duplicate(int[] numbers,int length,int[] duplication) { 24 if (numbers == null || length==0) 25 return false; 26 27 Arrays.sort(numbers); 28 29 int temp=0,j = 0,duplen=0; 30 for(int i=0;i<length-1;i++){ 31 if (numbers[i] == numbers[i+1]){ 32 temp++; 33 /* 34 * 當且僅當兩個相鄰的值相同一次(temp == 1)便積累重復數組duplication中去, 35 * 如接連有三個以上的相鄰值相同如四個2{2,2,2,2}共有三次比較, 36 * 第一次{2,2}比較時候,temp++==1,可存入duplication數組,{2,2,2}后兩次比較temp++==2,temp++==3 37 * temp的值均不滿足條件if (temp == 1),故duplication不會之前已存的數據 38 * */ 39 if (temp == 1){ 40 duplication[j++] = numbers[i]; 41 duplen++; 42 } 43 }else{ 44 temp = 0; } 45 } 46 if(duplen == 0){ 47 return false; 48 }else { 49 return true; 50 } 51 } 52 }
2)思路二實現
1 import java.util.*; 2 3 public class TEST{ 4 public static void main(String[] args) { 5 TEST t = new TEST(); 6 int[] numbers = {1,3,4,5,5,3,2,3,2,2,2}; 7 int[] dup = new int[numbers.length/2+1]; 8 9 System.out.println("原numbers數組:"); 10 for (int i = 0; i < numbers.length; i++) { 11 System.out.println("numbers["+i+"]:"+numbers[i]); 12 } 13 14 if(t.duplicate(numbers,numbers.length,dup)) 15 { 16 System.out.println("含有重復元素dup:"); 17 for (int i = 0; i < dup.length; i++) { 18 System.out.println("dup["+i+"]:"+dup[i]); 19 } 20 } 21 22 } 23 public boolean duplicate(int[] numbers,int length,int[] duplication) { 24 if (numbers == null || length==0) 25 return false; 26 27 Arrays.sort(numbers); 28 29 int temp=0,j = 0,duplen=0; 30 Set<Integer> set = new HashSet(); 31 for(int i=0;i<length-1;i++){ 32 if (numbers[i] == numbers[i+1] && !set.contains(numbers[i])){ 33 set.add(numbers[i]); 34 } 35 } 36 Iterator<Integer> iterator = set.iterator(); 37 var tempdup = 0; 38 while (iterator.hasNext()){ 39 tempdup = iterator.next(); 40 System.out.println("set:"+tempdup); 41 duplication[j++] = tempdup; 42 duplen++; 43 } 44 if(duplen == 0){ 45 return false; 46 }else { 47 return true; 48 } 49 } 50 }