# 如何确定重复整数的数量及其计数？How Determine the number of repeated integers and their counts?

1：2倍4：3倍4：2倍22：2倍

``````import java.util.Scanner;

public class repeated_elements {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.nextInt();

// The Array
int[]Array = new int[size];

// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for(int i =0; i<size; i++){
Array[i]=input.nextInt();
}

// Sorting The Array (Ascending Order)
for(int j = 0; j<size; j++){

int mini = Array[j];
int mini_index = j;

for(int i = j; i<size; i++){
if(Array[i] < mini){
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}

// Count The Repeated Numbers
for(int i=0; i<size; i++){

int key = Array[i];
int counter = 0;

for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}
}

if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
}
}
``````

## 5 个回复5

### #1楼 票数：1 已采纳

``````   // Count The Repeated Numbers
for(int i=0; i<size; i++){

int key = Array[i];
int counter = 0;

for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}
}

if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
``````

``````   // Count The Repeated Numbers
for(int i=0; i<size; ){   //<--- note the removal of i++

int key = Array[i];
int counter = 0;

for(int j=i; j<size; j++){
if(key == Array[j]){
counter++;
}else{
i=j;
break;
}
}

if(counter > 1 ){
System.out.println(key+":"+counter+" times ");
}
}
``````

``````1 1 1 1
``````

``````1:4
1:3
1:2
``````

``````11
13 34 22 4 499 4 22 18 4 1 1
``````

``````1:2 times
4:3 times
22:2 times
``````

### #2楼 票数：0

``````public static void main(String[] args) {
int[] input = {1,1,2,2,2,0};
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.length;

// The Array
int[]Array = new int[size];

// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for(int i =0; i<size; i++){
Array[i]=input[i];
}

// Sorting The Array (Ascending Order)
for(int j = 0; j<size; j++){

int mini = Array[j];
int mini_index = j;

for(int i = j; i<size; i++){
if(Array[i] < mini){
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}

// Count The Repeated Numbers
for(int i=0; i<size; i++){

int key = Array[i];
int counter = 0;

for(int j=0; j<size; j++){
if(key == Array[j]){
counter++;
}
}

if(counter > 0 ){
System.out.println(Array[i]+":"+counter+" times ");
}
}
}
``````

}

### #4楼 票数：0

``````if(counter > 1 ){
System.out.println(Array[i]+":"+counter+" times ");
i +=counter;
}
``````

### #5楼 票数：0

``````public class RepeatedElements {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// User Choose The Array Size
System.out.println("Enter Array Size: ");
int size = input.nextInt();

// The Array
int[] Array = new int[size];

// Read The Array Elements From The User
System.out.println("Enter Array Elements:");
for (int i = 0; i < size; i++) {
Array[i] = input.nextInt();
}

// Sorting The Array (Ascending Order)
for (int j = 0; j < size; j++) {

int mini = Array[j];
int mini_index = j;

for (int i = j; i < size; i++) {
if (Array[i] < mini) {
mini = Array[i];
mini_index = i;
}
}
int tmp = Array[j];
Array[j] = Array[mini_index];
Array[mini_index] = tmp;
}
//      System.out.println("sorted array:"+Arrays.asList(Array));
int key[] = new int[Array.length];
int keylen = 0;
// Count The Repeated Numbers
for (int i = 0; i < size; i++) {

int found = 0;
int k;
for ( k=0;k<keylen;k++){
if (key[k]==Array[i]){
found=1;
}
}
//      11  System.out.println("k value"+k+"i value"+i);
if (found==1)
else
key[keylen]=Array[i];

int counter = 0;

for (int j = i; j < size; j++) {
if (key[keylen] == Array[j]) {
counter++;
}
}
keylen++;
if (counter > 1) {
System.out.println(Array[i] + ":" + counter + " times ");
}
}

}
}
``````

3回复

3回复

4回复

3回复

4回复

6回复

2回复

3回复