1

I am very new to java and a junior java developer. The requirement asks to get the list of customerid and if the list contains customer ids which are repeated, then it would display the dupliate record. And if there are no customer id which are repeated then it would display no duplicate record. I have tried but stuck at the point where the customerid is repeated more than twice. My code works fine till the point the customerid is repeated twice. For ex:

Customerid:(5 input)
123
123
234
123
234
Output(Expected)
123
234
Actual:
123
123
123
234

for Scenario where there is no duplicate element, it would print no records found. Input: 123 234 345 456 567 Output: No records found

Output of my code is wrong when the repetition is more than twice. Kindly Advice. Code:

package firstpack;
import java.util.Scanner;
public class Class9 {

    public static void main(String[] args) {
        int a, i;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        long[] b = new long[a];
        for (i = 0; i <= (b.length - 1); ) {
            do {
                b[i] = sc.nextLong();
                i++;
            }
            while (i <= a - 1);
        }
        System.out.println("Duplicate records :");

        for (i = 0; i < b.length - 1; i++) {
            for (int j = i + 1; j < b.length; j++) {
                if ((b[i] == (b[j])) && (i != j)) {
                    System.out.println(b[j]);
                }
            }
        }
    }
2
  • 1
    Inside the two nested for-loops you have an if condition, and every time you find a duplicate - you're printing it. So if a dup exists more than twice - you'll print it more times... What you probably want to do instead is: every time you run into a dup - add it to a HashSet. The set will avoid duplications - so when you try to insert the same thing twice - it won't work and the set will retain only one copy. After you're done with the for-loops, simply print out the set. Commented Nov 21, 2017 at 7:24
  • Hi Alfasin,Thanks for the response. Commented Nov 21, 2017 at 7:56

5 Answers 5

3
//Try this code if you don't want to use set for Array uniqueness.

import java.util.*;
class MyUnique{
public static void main(String args[]){

Scanner sc= new Scanner(System.in);
ArrayList<Integer> arr= new ArrayList<Integer>();
    int length=sc.nextInt();
    int myarr[]= new int[length];
    for(int i=0;i<length;i++){
        System.out.println("enter the value of array");
        int value=sc.nextInt();
        if(arr.contains(value)){
            System.out.println("dublicate value not add and index not increased");
            i = i-1; //index will not increse
        }else{
            myarr[i] = value;
        }
        arr.add(value);
    }
    System.out.println("Here is the array output");
    for(int m=0;m<length; m++){
        System.out.print(myarr[m]+",");
    }
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Anuj,Thanks for your response
1

As commented by alfasin, you should use HashSet as they don't take duplicate values. So you don't have to use any loops to check. After that just print the set and you will get the result. So use something like this :

Set<Integer> s = new HashSet<Integer>();
s.add(123);
s.add(123);
s.add(234);
s.add(123);
s.add(234);

for(Integer i : s) {
    System.out.println(i);
}

When I print it I get:

234
123

Hope that helps !!

1 Comment

Hi Raddix,Thanks for the reponse. I am working with a form which can take multiple entries for the user input customerids and any id can be repeated n number of times..
0

You can use LinkedHashSet to get the distinct list of inputs and counts how many same inputs are in the list.

public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int inputCount = sc.nextInt();
    List<Long> inputArr = new ArrayList<>();
    for (int i = 0; i < inputCount; i++){
        inputArr.add(sc.nextLong());
    }
    System.out.println("==================");
    Set<Long> distinctArr = new LinkedHashSet<>(inputArr);
    for (Long input : distinctArr) {
        if(Collections.frequency(inputArr, input) > 1){
            System.out.println(input);
        }
    }
}

Or you could just put your result in HashSet before print out.

Set<Long> set = new HashSet<Long>();
for (i = 0; i < b.length - 1; i++) {
    for (int j = i + 1; j < b.length; j++) {
        if ((b[i] == (b[j])) && (i != j)) {
            set.add(b[j]);
        }
    }
}
for (Long result: set) {
    System.out.println(result);
}

4 Comments

Hi Paul,Thanks for your response.
Hi Paul, HashSet has helped me to solve my issue. I do not know why i am stuck at the point where the code wld print no records found if their are no duplicate records. The if statement which i am using is not giving me an result
Hi Paul, I have updated the question as well...could you please assist me in this.
you could check size of the Set by set.size(). if size of set is 0 then it means there is no duplicate records!
0

Try this use set to find out if duplicate entry exists or not

public static void main(String[] args) throws IOException {

    int a, i;
    Scanner sc = new Scanner(System.in);
    a = sc.nextInt();
    long[] b = new long[a];
    for (i = 0; i <= (b.length - 1); ) {
        do {
            b[i] = sc.nextLong();
            i++;
        }
        while (i <= a - 1);
    }

    Set<Long> first = new HashSet<>();
    Set<Long> duplicates = new HashSet<>();

    for (i = 0; i < b.length ; i++) {
        if(!first.add(b[i])) {
            duplicates.add(b[i]);
        }
    }

    String message = duplicates.size() > 0 ? "Duplicate records:": "No Duplicate entry";
    System.out.println(message);
    duplicates.stream().forEach(System.out::println);

    //in case if you want to print unique entries
    first.removeAll(duplicates);
    if(first.size() > 0){
        System.out.println("Unique Entries:");
        first.stream().forEach(System.out::println);
    }
}

5 Comments

Hi,Thanks for your response.But when i try to execute the above code, it gives a single output. Say if the numbers are 123,123,234,234,123..The output should be 123 and 234..but it is displayed as 123 only
234 is not a duplicate in the given list the program will give you only the duplicates. You also need non duplicates as well i.e. the entries that are found only once?
I have updated the program to print duplicate and single entry. Please try
Hi Ravik, Thanks for the response. I need to print the duplicate values in case they are in the list. If there are no duplicate values in the list then it would print no records found.
Please edit your question that have expected outcome for each scenario to avoid any confusions.
0

You can use HashMap to store the count of each element and print only if the count is more than 1. And if you need in the order of insertion then use LinkedHashMap instead of HashMap.

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Class9 {

    public static void main(String[] args) {
        int a, i;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        long[] b = new long[a];
        Map<Long, Integer> countMap = new HashMap<Long, Integer>();

        for (i = 0; i <= (b.length - 1); ) {
            do {
                b[i] = sc.nextLong();
                if (countMap.containsKey(b[i])) {
                    countMap.put(b[i], countMap.get(b[i]) + 1);
                } else {
                    countMap.put(b[i], 1);
                }
                i++;
            }
            while (i <= a - 1);
        }
        System.out.println("Duplicate records :");

        for (Long key : countMap.keySet()) {
            if (countMap.get(key) > 1) {
                System.out.println(key);
            }
        }


    }
}

Comments

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.