英文:
N times problem while iterating through loop in Duplicate element program
问题
import java.util.*;
public class TestClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] val = new String[n];
scan.nextLine();
for (int i = 0; i < n; i++) {
val[i] = scan.nextLine();
}
boolean duplicateFound = false;
for (int i = 0; i < n; i++) {
boolean hasDuplicate = false;
for (int j = i + 1; j < n; j++) {
if (val[i].equals(val[j])) {
hasDuplicate = true;
break;
}
}
if (!hasDuplicate) {
duplicateFound = true;
System.out.println("No duplicate found");
break;
}
}
if (!duplicateFound) {
System.out.println("All values are duplicates");
}
}
}
英文:
import java.util.*;
public class TestClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] val = new String[n];
scan.nextLine();
for (int i = 0; i < n; i++) {
val[i] = scan.nextLine();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (val[i].equals(val[j])) {
System.out.println(val[i]);
}
}
}
}
}
This is a simple code for finding duplicate array value but I need an else part where it should print "No duplicate found" but the problem is as I am iterating it through a loop it's printing N times the output.
INPUT
cat
dog
frog
owl
OUTPUT
No duplicate found
答案1
得分: 1
你可以使用一个检查变量,例如
boolean duplicatefound = false;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (val[i].equals(val[j]))
{
System.out.println(val[i]);
duplicatefound = true;
}
}
}
if (duplicatefound)
{
System.out.println("发现重复项");
}
else
{
System.out.println("未发现重复项");
}
英文:
you can have a check variable for example
boolean duplicatefound = false;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (val[i].equals(val[j]))
{
System.out.println(val[i]);
duplicatefound = true;
}
}
}
if(duplicatefound)
{
System.out.println("duplicate found");
}else
{
System.out.println("No Duplicated found");
}
答案2
得分: 1
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
public class TestClass
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] val = new String[n];
scan.nextLine();
for (int i = 0; i < n; i++)
{
val[i] = scan.nextLine();
}
boolean dups = false;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (val[i].equals(val[j]))
{
System.out.println(val[i]);
} else
{
dups = true;
}
}
}
if (dups)
{
System.out.println("no duplicate found");
}
if (findDups(val))
{
System.out.println("no duplicate found");
}
if (findDups(Arrays.asList(val)))
{
System.out.println("no duplicate found");
}
}
// different approach to avoid nested loops
public static boolean findDups(String[] arr)
{
Set<String> set = new HashSet<>();
for (String i : arr)
{
if (set.contains(i))
{
return false;
} else
{
set.add(i);
}
}
return true;
}
// Java 8 streams
public static boolean findDups(List<String> list)
{
return list.stream().filter(i -> Collections.frequency(list, i) > 1)
.collect(Collectors.toSet()).isEmpty();
}
}
英文:
Can we use something like this
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
public class TestClass
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] val = new String[n];
scan.nextLine();
for (int i = 0; i < n; i++)
{
val[i] = scan.nextLine();
}
boolean dups = false;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (val[i].equals(val[j]))
{
System.out.println(val[i]);
} else
{
dups = true;
}
}
}
if (dups)
{
System.out.println("no duplicate found");
}
if (findDups(val))
{
System.out.println("no duplicate found");
}
if (findDups(Arrays.asList(val)))
{
System.out.println("no duplicate found");
}
}
// different approach to avoid nested loops
public static boolean findDups(String[] arr)
{
Set<String> set = new HashSet<>();
for (String i : arr)
{
if (set.contains(i))
{
return false;
} else
{
set.add(i);
}
}
return true;
}
// Java 8 streams
public static boolean findDups(List<String> list)
{
return list.stream().filter(i -> Collections.frequency(list, i) > 1)
.collect(Collectors.toSet()).isEmpty();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论