在重复元素程序中,在循环中迭代时出现的N次问题。

huangapple go评论63阅读模式
英文:

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 &lt; n; i++) {
      val[i] = scan.nextLine();
    }
    for (int i = 0; i &lt; n; i++) {
      for (int j = i + 1; j &lt; 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 &lt; n; i++)
        {
            for (int j = i + 1; j &lt; n; j++)
            {
                if (val[i].equals(val[j]))
                {
                    System.out.println(val[i]);
                    duplicatefound = true;
                }
            }
        }

        if(duplicatefound)
        {
            System.out.println(&quot;duplicate found&quot;);
        }else
        {
            System.out.println(&quot;No Duplicated found&quot;);
        }

答案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 &lt; n; i++)
{
val[i] = scan.nextLine();
}
boolean dups = false;
for (int i = 0; i &lt; n; i++)
{
for (int j = i + 1; j &lt; n; j++)
{
if (val[i].equals(val[j]))
{
System.out.println(val[i]);
} else
{
dups = true;
}
}
}
if (dups)
{
System.out.println(&quot;no duplicate found&quot;);
}
if (findDups(val))
{
System.out.println(&quot;no duplicate found&quot;);
}
if (findDups(Arrays.asList(val)))
{
System.out.println(&quot;no duplicate found&quot;);
}
}
// different approach to avoid nested loops
public static boolean findDups(String[] arr)
{
Set&lt;String&gt; set = new HashSet&lt;&gt;();
for (String i : arr)
{
if (set.contains(i))
{
return false;
} else
{
set.add(i);
}
}
return true;
}
// Java 8 streams
public static boolean findDups(List&lt;String&gt; list)
{
return list.stream().filter(i -&gt; Collections.frequency(list, i) &gt; 1)
.collect(Collectors.toSet()).isEmpty();
}
}

huangapple
  • 本文由 发表于 2020年10月12日 11:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64311147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定