0
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

2 Answers 2

1

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");
        }
Sign up to request clarification or add additional context in comments.

Comments

1

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();

    }
    
}

2 Comments

I tried every way and it worked since I'm new to java I need to learn list and HashSet. Is there any other useful source where I could learn these fast?
@Jack, I would suggest you to follow http://tutorials.jenkov.com/ for understanding the some basic concepts of JAVA. It worked for me. All the Best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.