Java 是否仅打印数组中的非重复元素?
这是我的代码片段Java 是否仅打印数组中的非重复元素?,java,arrays,Java,Arrays,这是我的代码片段 import java.util.*; public class UniqueEl { public static void main(String []p) { Scanner sc=new Scanner(System.in); System.out.println("Enter Array size"); int size=sc.nextInt(); //boolean ischeck=tru
import java.util.*;
public class UniqueEl
{
public static void main(String []p)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Array size");
int size=sc.nextInt();
//boolean ischeck=true;
int flag=0,cnt=0;
int []num=new int[size];
System.out.println("Enter Array Elements");
for(int i=0;i<size;i++)
{
num[i]=sc.nextInt();
}
System.out.println("Display Array Elements");
for(int i=0;i<size;i++)
{
System.out.println("Array Elements are :-"+num[i]);
}
System.out.println("Unique elements from the array ");
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i!=j)
{
if(num[i]=num[j])
{
flag=1;
}
else
{
flag=0;
break;
}
}
}
if(flag==1)
{
cnt++;
System.out.println(num[i]+" ");
}
}
}
}
import java.util.*;
公共类Uniquel
{
公共静态void main(字符串[]p)
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入数组大小”);
int size=sc.nextInt();
//布尔值ischeck=true;
int标志=0,cnt=0;
int[]num=新的int[size];
System.out.println(“输入数组元素”);
对于(int i=0;i一种更简单的方法可能是使用Java 8的流功能来计算每个元素的外观数量,然后过滤非唯一的外观:
List<Integer> uniqueElements =
Arrays.stream(num)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() == 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
列出单一元素=
Arrays.stream(num)
.boxed()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet()
.stream()
.filter(e->e.getValue()==1)
.map(map.Entry::getKey)
.collect(Collectors.toList());
也许这会有帮助:
static int[] uniqueElementsFrom(int[] arr) {
final Map<Integer, Integer> numberOfOccurences = new HashMap<Integer, Integer>();
for (int i : arr) {
if (!numberOfOccurences.containsKey(i)) {
numberOfOccurences.put(i, 1);
} else {
numberOfOccurences.put(i, numberOfOccurences.get(i) + 1);
}
}
final Set<Integer> integers = numberOfOccurences.keySet();
List<Integer> uniques = new LinkedList<Integer>();
for (int i: integers) {
if (numberOfOccurences.get(i) == 1) {
uniques.add(i);
}
}
final int[] uniqueIntsArray = new int[uniques.size()];
for (int counter = 0; counter < uniques.size(); counter++) {
uniqueIntsArray[counter] = uniques.get(counter);
}
return uniqueIntsArray;
}
静态int[]uniquelementsfrom(int[]arr){
final Map NumberOfOccurrences=新HashMap();
用于(int i:arr){
如果(!numberofoccurrences.containsKey(i)){
发生次数。put(i,1);
}否则{
numberofoccurrencess.put(i,numberofoccurrences.get(i)+1);
}
}
最终设置整数=numberOfOccurences.keySet();
List uniques=new LinkedList();
for(整数i:整数){
if(numberofoccurrencess.get(i)==1){
添加(i);
}
}
final int[]uniqueIntsArray=新int[uniques.size()];
对于(int counter=0;计数器
您可以使用两个映射来存储找到的/放弃的值,因此只能迭代数组一次
方法:
Set getUniqueValues(int[] numbers) {
HashMap<Integer,Boolean> numIndex = new HashMap<Integer, Boolean>();
HashMap<Integer,Boolean> abandoned = new HashMap<Integer, Boolean>();
for (int i = 0; i < numbers.length; i++) {
int currentNumber = numbers[i];
try {
// check if already abandoned and skip this iteration
if ( abandoned.get(currentNumber) != null) continue;
} catch(Exception e) {
}
boolean isInIndex;
try {
// check if it is already indexed
isInIndex = numIndex.get(currentNumber);
} catch(Exception e) {
// if not, we found it the first time
isInIndex = false;
}
if (isInIndex == false){
//so we put it to the index
numIndex.put(currentNumber, true);
}else{
// if it appeared, we abandon it
numIndex.remove(currentNumber);
abandoned.put(currentNumber, true);
}
}
return numIndex.keySet();
}
- 对于数组中的每个元素
- 元素是否已索引(已找到)
- 如果没有索引,则将其(索引到HashMap)
- 如果是,则将其从索引中删除并放在放弃列表中
- 结果是索引映射的键
代码:
Set getUniqueValues(int[] numbers) {
HashMap<Integer,Boolean> numIndex = new HashMap<Integer, Boolean>();
HashMap<Integer,Boolean> abandoned = new HashMap<Integer, Boolean>();
for (int i = 0; i < numbers.length; i++) {
int currentNumber = numbers[i];
try {
// check if already abandoned and skip this iteration
if ( abandoned.get(currentNumber) != null) continue;
} catch(Exception e) {
}
boolean isInIndex;
try {
// check if it is already indexed
isInIndex = numIndex.get(currentNumber);
} catch(Exception e) {
// if not, we found it the first time
isInIndex = false;
}
if (isInIndex == false){
//so we put it to the index
numIndex.put(currentNumber, true);
}else{
// if it appeared, we abandon it
numIndex.remove(currentNumber);
abandoned.put(currentNumber, true);
}
}
return numIndex.keySet();
}
设置getUniqueValues(int[]数字){
HashMap numIndex=新的HashMap();
HashMap放弃=新建HashMap();
for(int i=0;i
进一步阅读:
Set getUniqueValues(int[] numbers) {
HashMap<Integer,Boolean> numIndex = new HashMap<Integer, Boolean>();
HashMap<Integer,Boolean> abandoned = new HashMap<Integer, Boolean>();
for (int i = 0; i < numbers.length; i++) {
int currentNumber = numbers[i];
try {
// check if already abandoned and skip this iteration
if ( abandoned.get(currentNumber) != null) continue;
} catch(Exception e) {
}
boolean isInIndex;
try {
// check if it is already indexed
isInIndex = numIndex.get(currentNumber);
} catch(Exception e) {
// if not, we found it the first time
isInIndex = false;
}
if (isInIndex == false){
//so we put it to the index
numIndex.put(currentNumber, true);
}else{
// if it appeared, we abandon it
numIndex.remove(currentNumber);
abandoned.put(currentNumber, true);
}
}
return numIndex.keySet();
}
映射使用包装类(整型、布尔型),这些类由Java自动转换:
函数返回一个集合,该集合可以转换为数组:
如果要更正当前代码,我只看到两个问题:
1.如果(num[i]==num[j])要进行相等性检查,请使用==因为=是赋值运算符,并且要将num[i]与num[j]进行比较。
2.当您发现任何整数重复时,即flag=1,从内部循环中断。当flag=0时,表示此数字没有重复,您可以继续。请参阅下面的更正代码:
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i!=j)
{
if(num[i]==num[j])
{
flag=1; //it is repeated number
break; //break the loop as we already found a repetition of this number
}
}
}
if(flag==0)
{
cnt++;
System.out.println(num[i]+" "); //here is your non-repeated number
}
}
for(int i=0;i使用EXOR操作。仅当重复计数为偶数且只有1个唯一数字时有效
public class MyClass {
public static void main (String[] args)
{
int arr[] = { 1, 2, 5, 4, 6, 8, 9, 2, 1, 4, 5, 8, 9 };
int n = arr.length;
int v = 0;
for(int i = 0 ; i< n ; i++ ){
v = v ^ arr[i]; //XOR Operation
}
System.out.print(v);
}
}
公共类MyClass{
公共静态void main(字符串[]args)
{
int arr[]={1,2,5,4,6,8,9,2,1,4,5,8,9};
int n=阵列长度;
int v=0;
对于(int i=0;i