[英]How do you detect and delete more than two duplicates in an Array?
如何檢測和刪除數組中是否有兩個以上重復項。 在下面的示例中,我采用了元素為“ 10、20、30、40、40、40、50、60、70、80、10”的數組。 在這里,重復40次三次,重復10次兩次。 我可以編寫一個程序來檢測兩個重復項,但無法將40(重復三次)縮小一次。 注意:- 我要這樣做而不使用任何Java集合
public class ArrayDuplicate {
public void run1()
{
int[] a = {10, 20, 30, 40, 40, 40, 50, 60, 70, 80 ,10};
int size=a.length;
System.out.println("Array size before duplicate deletion "+size);
for(int i =0;i<(size-1);i++)
{
for(int j=i+1;j<=(size-1);j++)
{
if(a[i]==a[j] &&i!=j)
{
while(j<(size-1))
{
a[j]=a[j+1];
j++;
}
size--;
}
}
}
System.out.print("The array after deleting the duplicates is ");
for(int k=0;k<=(size-1);k++)
{
System.out.print(a[k]); //40 is being printed twice
if(k<(size-1))
{
System.out.print(",");
}
else
System.out.print(".");
}
}
public static void main(String[] args)
{
ArrayDuplicate ob = new ArrayDuplicate();
ob.run1();
}
}
您說過,您只想為列表中出現3次以上 (而不是2次)的元素保留一個元素。 如果它們等於或大於2,則TreeSet將是您唯一需要的東西。
您說過出現3次以上后,您只想保留1個。
這個給你:
int[] input = whatever_your_input;
List<Integer> result = new ArrayList<>();
Map<Integer, Integer> valueCounter = new HashMap<>();
for (int i=0; i<input.length; i++) {
int counter = valueCounter.get(input[i]);
if (counter = null) {
counter = 0;
}
valueCounter.put(input[i], counter++);
if (counter <3) {
result.add(input[i]);
}
}
結果將包含輸出列表。
編輯:只是要清楚一點,如果您根本不想要任何重復項,則只需要這樣做:
int[] input = whatever_your_input;
Set<Integer> result = new TreeSet<>();
for (int i=0; i<input.length; i++) {
result.add(input[i]);
}
結果將包含原始列表,沒有任何重復,並保持相同的排序。
Edit2:好的,OP似乎根本不需要重復。 另外,沒有要使用的java集合。 開始了:
int[] input = whatever_your_input;
int[] tmpResult = new int[input.length];
int tmpLength = 0;
for (int i=0; i<input.length; i++) {
boolean duplicated = false;
for (int j=0; j<tmpLength; j++) {
if (tmpResult[j] == input[i]) {
duplicated = true;
break;
}
}
if (!duplicated) {
tmpResult[tmpLength] = input[i];
tmpLength++;
}
}
int[] result = new int[tmpLength];
System.arraycopy(tmpResult, 0, result, 0, tmpLength);
結果將包含相同順序的值,並且沒有重復項。
int[] a = {10, 20, 30, 40, 40, 40, 50, 60, 70, 80 ,10};
Set<Integer> set = new HashSet<Integer>();
for (int i : a) {
set.add(i);
}
a = set.toArray(new int[set.size()]);
希望對您有所幫助!
我認為OP不想使用集合來刪除重復項。 所以我只是修改了他的代碼,如下所示。
for (int i = 0; i < (size - 1); i++) {
for (int j = i + 1; j <= (size - 1); j++) {
if (a[i] == a[j] && i != j) {
while (j < (size - 1)) {
a[j] = a[j + 1];
j++;
}
i--;//recheck in inner for loop is performed (i--) , to check adjacent duplicate element.
size--;
}
}
}
在這里,在內部for循環中執行了重新檢查(i--) ,以檢查相鄰的重復元素。
希望下面的代碼能做到。
public static void main(String[] args) {
int[] a = { 10, 20, 30, 40, 40, 40, 50, 60, 70, 80, 10 };
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
int count = 1;
if (m.containsKey(a[i])) {
m.put(a[i], m.get(a[i]) + 1);
}
else {
m.put(a[i], count);
}
}
for (int i = 0; i < a.length; i++) {
if (m.get(a[i]) > 2) {
} else {
al.add(a[i]);
}
}
System.out.println(al);
}
我知道為什么40重復兩次。在您的循環中,當i = 3且j = 4時,a [i] = a [j],因此在while循環中,數組向左移動,
a [4] = a [5],a [5] = a [6],a [6] = a [7],a [7] = a [8] ...
然后,
a [] = {10,20,30,40,40,50,60,70,80,10},大小= 9,j = 9。
繼續for循環,i = 4,j = 5,但是當i = 3時,a [3] = a [4] = 40,因此循環不能將40(重復三次)收縮一次。 我修改了您的代碼,如下所示。
public class ArrayDuplicate {
public void run1()
{
int[] a = {10, 20, 30, 40, 40, 40, 50, 40, 60, 70, 80 ,10};
int size=a.length;
System.out.println("Array size before duplicate deletion "+size);
for(int i =0;i<(size-1);i++)
{
for(int j=i+1;j<=(size-1);j++)
{
if(a[i]==a[j] &&i!=j)
{
int temp = j;
while(temp<(size-1))
{
a[temp]=a[temp+1];
temp++;
}
size--;
j=i;
}
}
}
System.out.print("The array after deleting the duplicates is ");
for(int k=0;k<=(size-1);k++)
{
System.out.print(a[k]); //40 is being printed twice
if(k<(size-1))
{
System.out.print(",");
}
else
System.out.print(".");
}
}
public static void main(String[] args)
{
ArrayDuplicate ob = new ArrayDuplicate();
ob.run1();
}
}
Set
實現不能保存重復項,因此,通過將所有值插入Set
所有重復項都將被刪除。
使用Java 8 Streams和Collections可以像這樣實現:
// Remove all duplicates by inserting into a Set
Set<Integer> noDupes = Arrays.stream(a).boxed().collect(Collectors.toCollection(TreeSet::new));
// Set to primitive array
a = noDupes.stream().mapToInt(Integer::intValue).toArray();
那么a
將保留原始值減去重復項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.