Sayantan
Sayantan

Reputation: 7

Need to find out the duplicate element in array without using Hashmaps

I am a newbie here. I wanted to print out the duplicate elements in an array.

This code will print out the duplicate elements. Suppose I'm taking an array of size 5 with elements [1,2,5,5,5] This code will print:

Duplicate elements: 5,5,5 //(since 5 is being repeated thrice.)

But I want the output something like this

Duplicate Elements: 5 //( instead of printing 5 thrice)

import java.util.*;
import java.util.Scanner;

public class duplicateArray{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int x =sc.nextInt();
        int arr[]=new int[x];
        int i,count=0;
            for(i=0;i<x;i++){
                arr[i]=sc.nextInt();
            }
            System.out.print("Array: ");
            for(i=0;i<x;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println(" ");
        System.out.print("Duplicate elements: ");
        for(i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    System.out.print(arr[j]+" ");
                }
            }
        }
    }
}

Upvotes: 1

Views: 2263

Answers (5)

Drew Mash
Drew Mash

Reputation: 31

Without using Hashmaps, I think your best option would be to first sort the array and then count the duplicates. Since the array is now in order you can print the duplicates after each number switch!

If this is for an assignment go ahead and google bubble sort and implement it as a method.

Upvotes: 0

Poulami
Poulami

Reputation: 23

    System.out.println("Duplicate Elements : ");
    for(int i = 0; i<arr.length; i++){
        boolean isDuplicate = false;
        for(int k=0;k<i;k++){
            if(arr[i]== arr[k]){
                isDuplicate =  true;
                break;
            }
        }
        if(isDuplicate){
            continue;
        }
        int count = 0;
        for(int j=0; j<arr.length; j++){
            if(arr[i] == arr[j]){
                count++;
            }
            if(count >1){
                System.out.println(arr[i]);
                break;
            }
        }
    }

Upvotes: 0

Yoshikage Kira
Yoshikage Kira

Reputation: 1121

This is pretty simple however you need to sort array before. All you need to know if duplicate for an element exists or not and do the printing in the outside for loop. Rest is described in comments

Arrays.sort(arr); // Sort Array
for (int i = 0; i < arr.length; i++) {
    boolean hasDuplicate = false; // Assume that arr[i] is not repeating
    for (int j = i + 1; j < arr.length; j++) {
        // Check if it is repeating
        if (arr[i] == arr[j]) {
            // If it repeats
            hasDuplicate = true;
        }
        // Since array is sorted we know that there is no value of arr[i] after this
        if (arr[i] != arr[j]) {
            // Set i to the last occurrence of arr[i] value
            i = j - 1;
            break; // Since there no occurrence of arr[i] value there is no need to continue
        }
    }
    // Print the element at i
    if (hasDuplicate)
        System.out.print(arr[i] + " ");
    // In next iteration loop will start from the index next to the last occurrence of value of arr[i]
}

Upvotes: 0

DodgyCodeException
DodgyCodeException

Reputation: 6123

The following code does it without creating any additional data structure. For each element, it counts the number of duplicates previously encountered and only prints the first duplicate.

If I were doing this in the real world, I would use a Set but I'm assuming you haven't learnt about them yet, so I'm only using the array that you've already created.

import java.util.Scanner;

public class DuplicateArray {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int x = sc.nextInt();
        int[] arr = new int[x];

        System.out.print("Enter " + x + " values: ");
        for (int i = 0; i < x; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.print("Array: ");
        for (int i = 0; i < x; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();

        System.out.print("Duplicate elements:");
        for (int i = 0; i < arr.length; i++) {
            int numDups = 0;
            for (int j = 0; j < i; j++) {
                if (arr[i] == arr[j]) {
                    numDups++;
                }
            }
            if (numDups == 1) {
                System.out.print(" " + arr[i]);
            }
        }
        System.out.println();
    }
}

Upvotes: 3

Zephyr
Zephyr

Reputation: 10253

One solution is to create a separate List to store any duplicates found.

That, in addition to using the .contains() method of the List, you can ensure only one entry per int is made.

public static void main(String[] args) {

        // Sample array of ints
        int[] ints = {1, 1, 4, 5, 2, 34, 7, 5, 3};

        // Create a separate List to hold duplicated values
        List<Integer> duplicates = new ArrayList<>();

        // Find duplicates
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints.length; j++) {
                if (ints[i] == ints[j] && // Are the ints the same value?
                        i != j &&  // Ignore if we're looking at the same index 
                        !duplicates.contains(ints[i])) { // Check if our List of duplicates already has this entry
                    duplicates.add(ints[i]); // Add to list of duplicates
                }
            }
        }

        System.out.println("Duplicates: " + duplicates);

    }

Output:

Duplicates: [1, 5]

Upvotes: 0

Related Questions