1. Dada uma matriz contendo elementos repetidos (após o título), escreva um programa para descobrir quais elementos na matriz são repetidos e registre quantas vezes ela é repetida: public class ArrayExample {public static void main (String [] args ) {matriz interna = {1,6,3,6,7,9,8,6,7,8,8,0,4,5,2,9}; [Código suplementar]}
package com.temp;
import java.util.HashMap;
import java.util.HashSet;
/**
* @Author lanxiaofang
* @email [email protected]
* @date 2020/09/21 8:15
*/
public class ElemRepetition {
public static void main(String[] args) {
Integer[] array={1,6,3,6,7,9,8,6,7,8,8,0,4,5,2,9};
HashSet<Integer> hash_set = new HashSet<>();
//将数组中的值放入HashSet去重
for (Integer s : array){
hash_set.add(s);
}
// 将HashSet转为数组Array
Integer[] array_ = hash_set.toArray(new Integer[hash_set.size()]);
System.out.println("---- array_.length "+array_.length+" array.length "+array.length);
//使用HashMap来存储对应的值,以及对应的值出现的次数
HashMap<Integer, Integer> hash_map = new HashMap<>();
for(Integer i = 0; i < array_.length; i++){
int count = 0;
for(Integer j = 0; j < array.length; j++){
if(array_[i] == array[j])
count++;
hash_map.put(array_[i], count);
}
}
for(int i=0; i< array_.length; i++){
System.out.println(array_[i]+" -- "+hash_map.get(array_[i]));
}
}
}
Os resultados são os seguintes: