仅打印int数组中的重复项以及它们出现的次数

时间:2022-04-04 23:54:30

I am trying to print only the duplicate elements from an array and how many times each of them appear.

我试图只打印数组中的重复元素以及每个元素出现的次数。

Example:

Number 0 appears 3 times; 
Number 12 appears 2 times; 
Number 43 appears 2 times

My code prints all the elements of the array rather than printing only duplicate elements.

我的代码打印数组的所有元素,而不是只打印重复的元素。

I must mention that i cannot use HashMap, only conditional statments.
Thanks Below is the code :

我必须提一下,我不能使用HashMap,只能使用条件语句。谢谢以下是代码:

    int[] array = new int[] { 12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87 };
    int counter = 0, temp = 0;// variable that holds the temporary value of each element
    Arrays.sort(array);
    for (int i = 0; i < array.length; i++) {
        temp = array[i];
        counter = 0;
        for (int j = 0; j < array.length; j++) {
            if (temp == array[j]) {
                counter++;
            }
        }
        System.out.println("Number:" + array[i] + "occurs :" + counter + " times");
    }

5 个解决方案

#1


1  

You can it either with HashMap or with Simple sorting, as your per comments simple method is as below

您可以使用HashMap或使用简单排序,因为您的每条评论简单方法如下所示

  int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};
                int counter = 0, 
                temp = 0;//variable that holds the temporary value of each element
                Arrays.sort(array);
                temp=array[0];
                int count=1;
                for(int i=1;i<array.length;i++)
                {
                    //System.out.println(array[i]);
                    if(temp==array[i])
                    {
                        count++;
                    }
                    else
                    {
                        if(count==1)
                        {
                            count=1;

                        }
                        else{
                        System.out.println(array[i-1]+" "+count);
                        count=1;
                        }
                        temp=array[i];
                    }
                }

#2


1  

Try this:

package com.*.java;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class DuplicatesAndCount {
    public static void main(String[] args) {
    int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};

    // taking hashmap to accumulate corresponding couter of each number
     HashMap<Integer, Integer> ht = new HashMap<Integer, Integer>();                    
     for (Integer newNumber : array) {
        if (ht.get(newNumber) == null) {
            ht.put(newNumber, 1); 
        } else {
            ht.put(newNumber, ht.get(newNumber) + 1);
         }

    }

  System.out.println(ht);
 // now iterate the map
 for (Map.Entry<String, Integer> entry : map.entrySet()) {
    Integer number = entry.getKey();
    Integer count = entry.getValue();
    if (count > 1) {
        System.out.println("Number: " + number + "count: " + count);
    }
  }
}
}

#3


0  

One simple solution here would be to use a map to count the number of occurrences of each digit. Then, iterate the map and report any number having a count of two or more.

这里的一个简单解决方案是使用映射来计算每个数字的出现次数。然后,迭代地图并报告任何具有两个或更多计数的数字。

int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};
Map<Integer, Integer> map = new HashMap<>();
for (int num : array) {
    Integer count = map.get(num);
    map.put(num, count == null ? 1 : count.intValue() + 1);
}

// now iterate the map
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    Integer num = entry.getKey();
    Integer count = entry.getValue();
    if (count > 1) {
        System.out.println("Number: " + num + ", occurs: " + count + " times");
    }
}

Demo

#4


0  

Here is my version:

这是我的版本:

 Integer[] arr = {1,2,2,2,1,3,5,4,1};
 Arrays.stream(arr).distinct()
                   .forEach(x -> System.out.println(x + " appears " + Collections.frequency(Arrays.asList(arr), x) + " times."));

We remove duplicates from the game so we are left with unique elements, then for each unique element in our stream we print out how many time it appears in original array.

我们从游戏中移除重复项,因此我们留下了独特的元素,然后对于流中的每个唯一元素,我们打印出它在原始数组中出现的时间。

#5


0  

The question has many solutions. I will list a few of them as a starting point but leave the coding up to you.

这个问题有很多解决方案。我将列出其中一些作为起点,但请将编码留给您。

  1. Use a HashMap and store the <Number, Frequency> in the map.
  2. 使用HashMap并将 存储在地图中。 ,frequency>

  3. If you are not allowed to use maps, you could do a double loop and check for all occurrences. That would be O(n2).
  4. 如果不允许使用地图,则可以执行双循环并检查所有实例。那将是O(n2)。

  5. A slightly better modification would be to sort the array first and then compare the consecutive elements. This would be O(n log n).
  6. 稍微好一点的修改是首先对数组进行排序,然后比较连续的元素。这将是O(n log n)。

  7. You could use Collections.frequency() but that would operate on a list so convert your data structures (by Streams maybe).
  8. 您可以使用Collections.frequency(),但这将在列表上运行,因此转换您的数据结构(可能是Streams)。

Once you have used any of these methods, you can then filter out the numbers which have a frequency of 1.

一旦使用了这些方法中的任何一种,就可以过滤出频率为1的数字。

#1


1  

You can it either with HashMap or with Simple sorting, as your per comments simple method is as below

您可以使用HashMap或使用简单排序,因为您的每条评论简单方法如下所示

  int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};
                int counter = 0, 
                temp = 0;//variable that holds the temporary value of each element
                Arrays.sort(array);
                temp=array[0];
                int count=1;
                for(int i=1;i<array.length;i++)
                {
                    //System.out.println(array[i]);
                    if(temp==array[i])
                    {
                        count++;
                    }
                    else
                    {
                        if(count==1)
                        {
                            count=1;

                        }
                        else{
                        System.out.println(array[i-1]+" "+count);
                        count=1;
                        }
                        temp=array[i];
                    }
                }

#2


1  

Try this:

package com.*.java;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class DuplicatesAndCount {
    public static void main(String[] args) {
    int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};

    // taking hashmap to accumulate corresponding couter of each number
     HashMap<Integer, Integer> ht = new HashMap<Integer, Integer>();                    
     for (Integer newNumber : array) {
        if (ht.get(newNumber) == null) {
            ht.put(newNumber, 1); 
        } else {
            ht.put(newNumber, ht.get(newNumber) + 1);
         }

    }

  System.out.println(ht);
 // now iterate the map
 for (Map.Entry<String, Integer> entry : map.entrySet()) {
    Integer number = entry.getKey();
    Integer count = entry.getValue();
    if (count > 1) {
        System.out.println("Number: " + number + "count: " + count);
    }
  }
}
}

#3


0  

One simple solution here would be to use a map to count the number of occurrences of each digit. Then, iterate the map and report any number having a count of two or more.

这里的一个简单解决方案是使用映射来计算每个数字的出现次数。然后,迭代地图并报告任何具有两个或更多计数的数字。

int[] array = new int[]{12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87};
Map<Integer, Integer> map = new HashMap<>();
for (int num : array) {
    Integer count = map.get(num);
    map.put(num, count == null ? 1 : count.intValue() + 1);
}

// now iterate the map
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    Integer num = entry.getKey();
    Integer count = entry.getValue();
    if (count > 1) {
        System.out.println("Number: " + num + ", occurs: " + count + " times");
    }
}

Demo

#4


0  

Here is my version:

这是我的版本:

 Integer[] arr = {1,2,2,2,1,3,5,4,1};
 Arrays.stream(arr).distinct()
                   .forEach(x -> System.out.println(x + " appears " + Collections.frequency(Arrays.asList(arr), x) + " times."));

We remove duplicates from the game so we are left with unique elements, then for each unique element in our stream we print out how many time it appears in original array.

我们从游戏中移除重复项,因此我们留下了独特的元素,然后对于流中的每个唯一元素,我们打印出它在原始数组中出现的时间。

#5


0  

The question has many solutions. I will list a few of them as a starting point but leave the coding up to you.

这个问题有很多解决方案。我将列出其中一些作为起点,但请将编码留给您。

  1. Use a HashMap and store the <Number, Frequency> in the map.
  2. 使用HashMap并将 存储在地图中。 ,frequency>

  3. If you are not allowed to use maps, you could do a double loop and check for all occurrences. That would be O(n2).
  4. 如果不允许使用地图,则可以执行双循环并检查所有实例。那将是O(n2)。

  5. A slightly better modification would be to sort the array first and then compare the consecutive elements. This would be O(n log n).
  6. 稍微好一点的修改是首先对数组进行排序,然后比较连续的元素。这将是O(n log n)。

  7. You could use Collections.frequency() but that would operate on a list so convert your data structures (by Streams maybe).
  8. 您可以使用Collections.frequency(),但这将在列表上运行,因此转换您的数据结构(可能是Streams)。

Once you have used any of these methods, you can then filter out the numbers which have a frequency of 1.

一旦使用了这些方法中的任何一种,就可以过滤出频率为1的数字。