2014-03-30 42 views
1

所以我創建了一個隨機數組,我打印並計算重複的數字,現在我只需要創建一個新的數組與第一個相同的數字數組,但沒有任何重複。順便說一下,不能使用ArrayList。創建一個數組與舊的相同的數字,但沒有重複

我所擁有的是。

public static void main(String[] args) { 

    Random generator = new Random(); 
    int aR[]= new int[20]; 

    for(int i=0;i<aR.length;i++){ 
     int number=generator.nextInt(51); 
     aR[i]=number; 
     System.out.print(aR[i]+" "); 

    } 
    System.out.println(); 
    System.out.println(); 
    int countRep=0; 
    for(int i=0;i<aR.length;i++){ 
     for(int j=i+1;j<aR.length-1;j++){ 
      if(aR[i]==aR[j]){ 
       countRep++; 
       System.out.println(aR[i]+" "+aR[j]); 
       break; 
      } 
     } 
    } 
    System.out.println(); 
    System.out.println("Repeated numbers: "+countRep); 

    int newaR[]= new int[aR.length - countRep]; 




} 

有人可以幫忙嗎?

編輯:無法真正使用HashSet。此外,新陣列需要具有正確的大小。

+1

我猜如果你不允許使用'ArrayList'你也不能使用'HashSet',對吧? – Mureinik

+0

這聽起來很像我的作業問題。 – mralexlau

+0

這是什麼問題?如果不允許使用諸如集合或哈希等合理的東西,那麼它就是嵌套迭代的一個簡單問題,除非這些需求也聲明非複製數組必須是正確的大小。 –

回答

0

嘗試:

Set<Integer> insertedNumbers = new HashSet<>(newaR.length); 
int index = 0; 
for(int i = 0 ; i < aR.length ; ++i) { 
    if(!insertedNumbers.contains(aR[i])) { 
     newaR[index++] = aR[i]; 
    } 
    insertedNumbers.add(aR[i]); 
} 
+0

是否有一個特定的原因你恢復了編輯?你絕對*不需要執行包含檢查 - HashSet已經爲你做了。 –

+0

原因是「Set」不保留原始數組的順序。我同意你的看法,''HashSet''做檢查。這實際上是我使用它的原因:確保在保持原始順序的同時不插入相同的int兩次。 –

+0

如果你想保留原始數組的順序,那麼就使用LinkedHashSet。 –

1

使用Java 8和溪流,你可以做到以下幾點:

int[] array = new int[1024]; 
//fill array 
int[] arrayWithoutDuplicates = Arrays.stream(array) 
     .distinct() 
     .toArray(); 

這將:

  1. 將您的int[]IntStream
  2. 過濾掉所有重複項,因此保留不同的元素。
  3. 將其保存在類型爲int[]的新陣列中。
+0

+1。會有同樣的答案。 –

+0

這絕對是你應該在*生產*代碼(而不是*作業*代碼:-D)中做到的。出於好奇,我檢查了源代碼,並且實際上在引擎蓋下使用了LinkedHashSet。 –

0

一種可能的方法是通過在陣列行走,併爲每個值,計算在其再次發生在陣列中(這是-1,如果號碼不再出現)的索引。 不是再次出現的值的數量是唯一值的數量。然後收集相應索引爲-1的數組中的所有值。

import java.util.Arrays; 
import java.util.Random; 

public class UniqueIntTest 
{ 
    public static void main(String[] args) 
    { 
     int array[] = createRandomArray(20, 0, 51); 
     System.out.println("Array " + Arrays.toString(array)); 

     int result[] = computeUnique(array); 
     System.out.println("Result " + Arrays.toString(result)); 
    } 

    private static int[] createRandomArray(int size, int min, int max) 
    { 
     Random random = new Random(1); 
     int array[] = new int[size]; 
     for (int i = 0; i < size; i++) 
     { 
      array[i] = min + random.nextInt(max - min); 
     } 
     return array; 
    } 

    private static int[] computeUnique(int array[]) 
    { 
     int indices[] = new int[array.length]; 
     int unique = computeIndices(array, indices); 
     int result[] = new int[unique]; 
     int index = 0; 
     for (int i = 0; i < array.length; i++) 
     { 
      if (indices[i] == -1) 
      { 
       result[index] = array[i]; 
       index++; 
      } 
     } 
     return result; 
    } 

    private static int computeIndices(int array[], int indices[]) 
    { 
     int unique = 0; 
     for (int i = 0; i < array.length; i++) 
     { 
      int value = array[i]; 
      int index = indexOf(array, value, i + 1); 
      if (index == -1) 
      { 
       unique++; 
      } 
      indices[i] = index; 
     } 
     return unique; 
    } 

    private static int indexOf(int array[], int value, int offset) 
    { 
     for (int i = offset; i < array.length; i++) 
     { 
      if (array[i] == value) 
      { 
       return i; 
      } 
     } 
     return -1; 
    } 
} 
0

這聽起來像是一個家庭作業問題,如果是這樣的話,您應該選擇的技術是先排序數組。

一旦數組進行排序,重複的條目將是彼此相鄰,所以他們是平凡的發現:

int[] numbers = //obtain this however you normally would 
java.util.Arrays.sort(numbers); 

//find out how big the array is 
int sizeWithoutDuplicates = 1; //there will be at least one entry 
int lastValue = numbers[0]; 

//a number in the array is unique (or a first duplicate) 
//if it's not equal to the number before it 
for(int i = 1; i < numbers.length; i++) { 
    if (numbers[i] != lastValue) { 
     lastValue = i; 
     sizeWithoutDuplicates++; 
    } 
} 

//now we know how many results we have, and we can allocate the result array 
int[] result = new int[sizeWithoutDuplicates]; 

//fill the result array 
int positionInResult = 1; //there will be at least one entry 
result[0] = numbers[0]; 
lastValue = numbers[0]; 

for(int i = 1; i < numbers.length; i++) { 
    if (numbers[i] != lastValue) { 
     lastValue = i; 
     result[positionInResult] = i; 
     positionInResult++; 
    } 
} 

//result contains the unique numbers 

不能夠使用列表意味着我們必須弄清楚如何大的數組將會處於單獨的通道中 - 如果我們可以使用ArrayList來收集結果,那麼我們只需要通過數組數組進行單個循環。

這種方法比通過數組的雙重嵌套循環更快(O(n log n)與O(n^2))來查找重複。在O(n),使用HashSet會更快。

+0

如果這不是一個家庭作業問題,那麼你應該使用@skaiwi的答案 - 這是最簡單,最高效的方式,沒有理由*不*在現實生活中使用HashSet。 –

相關問題