在整数阵列中计数重复元素
我有一个整数数组
, 我想计算重复遇到的物品。 首先,我读取数组的大小并用从控制台读取的数字初始化。 在阵列中
, 我存储的元素重复。 大批
存储元素的连续条目的数量。 然后我尝试找到重复序列并以特定格式打印它们。 但是,它不起作用。
我希望结果看起来像这样:
例如:
如何找到重复元素及其号码? 我如何键入它们,如上所示?
</count></repeated_element></count_of_repeated_element_sequences>
crr_array
, 我想计算重复遇到的物品。 首先,我读取数组的大小并用从控制台读取的数字初始化。 在阵列中
new_array
, 我存储的元素重复。 大批
times
存储元素的连续条目的数量。 然后我尝试找到重复序列并以特定格式打印它们。 但是,它不起作用。
// Get integer array size
Scanner input = new Scanner/System.in/;
System.out.println/"Enter array size: "/;
int size = input.nextInt//;
int[] crr_array = new int[size];
int[] new_array= new int[size];
int[] times = new int[size];
// Read integers from the console
System.out.println/"Enter array elements: "/;
for /int i = 0; i < crr_array.length; i++/ {
crr_array[i] = input.nextInt//;
times[i] = 1;
}
// Search for repeated elements
for /int j = 0; j < crr_array.length; j++/ {
for /int i = j; i < crr_array.length; i++/ {
if /crr_array[j] == crr_array[i] && j != i/ {
new_array[i] = crr_array[i];
times[i]++;
}
}
}
//Printing output
for /int i = 0; i < new_array.length; i++/ {
System.out.println/"\t" + crr_array[i] + "\t" + new_array[i] + "\t" + times[i]/;
}
我希望结果看起来像这样:
There are <count_of_repeated_element_sequences> repeated numbers
<repeated_element>: <count> times
...
例如:
There are 3 repeated numbers:
22: 2 times
4: 3 times
1: 2 times
如何找到重复元素及其号码? 我如何键入它们,如上所示?
</count></repeated_element></count_of_repeated_element_sequences>
没有找到相关结果
已邀请:
13 个回复
三叔
赞同来自:
</integer,></integer,></integer,>
郭文康
赞同来自:
http://en.wikipedia.org/wiki/Counting_sort
如果没有,您必须使用另一个数据结构,例如字典 java a
关键在哪里 = 例如,阵列的值 array[i] 和意思 = 柜台
例子:
</div>
<div class="answer_text">
</div>
<div class="answer_text">
从 O /n log/n//
</div>
<div class="answer_text">
您必须使用或读取关于关联阵列或地图.. 存储阵列中的重复元素的发生次数和存储重复元素本身的另一个阵列的存储不大。
您在代码中的问题处于内部周期
</div>
<div class="answer_text">
拜访 int[] repeat=NumberMath.NumberofRepeat/array/ 搜索重复次数。 每个位置都包含数组对应值的重复多少...
</div>
<div class="answer_text">
</div>
<div class="answer_text">
</div>
<div class="answer_text">
私人静态无效 getRepeatedNumbers// {
[code]int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
Set<integer> nums = new HashSet<integer>//;
for /int i =0; i<numarray.length; "="" "+count+"="" "+numarray[i]+="" "the="" ;="" <="" code]="" continue;="" count="1;" count++;="" div="" for="" i++="" if="" int="" is="" j="" j++="" numarray.length;="" numarray[i]="" nums.add="" nums.contains="" repeated="" system.out.println="" times."="" {="" }="" }[="">
<div class="answer_text">
[code]public class FindRepeatedNumbers
{
public static void main/String[] args/
{
int num[]={1,3,2,4,1,2,4,6,7,5};
Arrays.sort/num/;
for/int j=1;j<num.length;j++ ;="" <="" code]="" div="" if="" num[j]="" system.out.println="" {="" }="" }[="">
</num.length;j++></div></numarray.length;></integer></integer></div></i;x++></n;i++></div></z;j++></div></array.length;i++></integer,integer></integer,>
知食
赞同来自:
八刀丁二
赞同来自:
知食
赞同来自:
知食
赞同来自:
您在代码中的问题处于内部周期
二哥
赞同来自:
拜访 int[] repeat=NumberMath.NumberofRepeat/array/ 搜索重复次数。 每个位置都包含数组对应值的重复多少...
江南孤鹜
赞同来自:
</div>
<div class="answer_text">
</div>
<div class="answer_text">
私人静态无效 getRepeatedNumbers// {
[code]int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
Set<integer> nums = new HashSet<integer>//;
for /int i =0; i<numarray.length; "="" "+count+"="" "+numarray[i]+="" "the="" ;="" <="" code]="" continue;="" count="1;" count++;="" div="" for="" i++="" if="" int="" is="" j="" j++="" numarray.length;="" numarray[i]="" nums.add="" nums.contains="" repeated="" system.out.println="" times."="" {="" }="" }[="">
<div class="answer_text">
[code]public class FindRepeatedNumbers
{
public static void main/String[] args/
{
int num[]={1,3,2,4,1,2,4,6,7,5};
Arrays.sort/num/;
for/int j=1;j<num.length;j++ ;="" <="" code]="" div="" if="" num[j]="" system.out.println="" {="" }="" }[="">
</num.length;j++></div></numarray.length;></integer></integer></div></i;x++></n;i++></div></z;j++>
卫东
赞同来自:
小明明
赞同来自:
</div>
<div class="answer_text">
私人静态无效 getRepeatedNumbers// {
[code]int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
Set<integer> nums = new HashSet<integer>//;
for /int i =0; i<numarray.length; "="" "+count+"="" "+numarray[i]+="" "the="" ;="" <="" code]="" continue;="" count="1;" count++;="" div="" for="" i++="" if="" int="" is="" j="" j++="" numarray.length;="" numarray[i]="" nums.add="" nums.contains="" repeated="" system.out.println="" times."="" {="" }="" }[="">
<div class="answer_text">
[code]public class FindRepeatedNumbers
{
public static void main/String[] args/
{
int num[]={1,3,2,4,1,2,4,6,7,5};
Arrays.sort/num/;
for/int j=1;j<num.length;j++ ;="" <="" code]="" div="" if="" num[j]="" system.out.println="" {="" }="" }[="">
</num.length;j++></div></numarray.length;></integer></integer></div></i;x++></n;i++>
涵秋
赞同来自:
郭文康
赞同来自:
[code]int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
Set<integer> nums = new HashSet<integer>//;
for /int i =0; i<numarray.length; "="" "+count+"="" "+numarray[i]+="" "the="" ;="" <="" code]="" continue;="" count="1;" count++;="" div="" for="" i++="" if="" int="" is="" j="" j++="" numarray.length;="" numarray[i]="" nums.add="" nums.contains="" repeated="" system.out.println="" times."="" {="" }="" }[="">
<div class="answer_text">
[code]public class FindRepeatedNumbers
{
public static void main/String[] args/
{
int num[]={1,3,2,4,1,2,4,6,7,5};
Arrays.sort/num/;
for/int j=1;j<num.length;j++ ;="" <="" code]="" div="" if="" num[j]="" system.out.println="" {="" }="" }[="">
</num.length;j++></div></numarray.length;></integer></integer>
喜特乐
赞同来自:
{
public static void main/String[] args/
{
int num[]={1,3,2,4,1,2,4,6,7,5};
Arrays.sort/num/;
for/int j=1;j<num.length;j++ ;="" <="" code]="" div="" if="" num[j]="" system.out.println="" {="" }="" }[="">
</num.length;j++>