1
public class ArrayTest{

    public static void main(String[] args) {

        int array[] = {32,3,3,4,5,6,88,98,9,9,9,9,9,9,1,2,3,4,5,6,4,3,7,7,8,8,88,88};

        for(int i= 0;i<array.length-1;i++){
            for(int j=i+1;j<array.length;j++){
                if((array[i])==(array[j]) && (i != j)){
                    System.out.println("element occuring twice are:" + array[j]);
                }
            }
        }
    }
}

this program work fine but when i compile it, it print the values again and again i want to print the duplicate value once for example if 9 is present 5 times in array so it print 9 once and if 5 is present 6 times or more it simply print 5...and so on....this what i want to be done. but this program not behave like that so what am i missing here.

your help would be highly appreciated. regards!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • read up on the "break" statement. – OldProgrammer Mar 16 '18 at 00:01
  • 1
    I don't think even a "break" will help. OP has to remember all previous duplicates, which is going to go beyond a "simple loop" imo. It's doable with a Map though. – markspace Mar 16 '18 at 00:03
  • Why do you think your code should do what you want? What is the specific problem you have with your code? As currently written your question asks for somebody to write code for you and doesn't belong on SO. – Oleg Mar 16 '18 at 00:10
  • 1
    If all you can use is a loop: when you find a value you think is a duplicate, loop through from 0 to `i-1`, checking if it contains the same value as `array[i]`. If it does, you have already printed that number, so you don't need to print it again. – Andy Turner Mar 16 '18 at 00:11
  • Possible duplicate: https://stackoverflow.com/questions/3951547/java-array-finding-duplicates – ozanonurtek Mar 16 '18 at 02:51

6 Answers6

0

Sort the array so you can get all the like values together.

public class ArrayTest{
    public static void main(String[] args) {
        int array[] = {32,3,3,4,5,6,88,98,9,9,9,9,9,9,1,2,3,4,5,6,4,3,7,7,8,8,88,88};
        Arrays.sort(array);
        for (int a = 0; a < array.length-1; a++) {
            boolean duplicate = false;
            while (array[a+1] == array[a]) {
                a++;
                duplicate = true;
            }
            if (duplicate) System.out.println("Duplicate is " + array[a]);
        }
    }
}
Jason Cheng
  • 92
  • 1
  • 6
0

I recommend using a Map to determine whether a value has been duplicated. Values that have occurred more than once would be considered as duplicates.

P.S. For duplicates, using a set abstract data type would be ideal (HashSet would be the implementation of the ADT), since lookup times are O(1) since it uses a hashing algorithm to map values to array indexes. I am using a map here, since we already have a solution using a set. In essence, apart from the data structure used, the logic is almost identical.

For more information on the map data structure, click here.

Instead of writing nested loops, you can just write two for loops, resulting in a solution with linear time complexity.

public void printDuplicates(int[] array) {
    Map<Integer, Integer> numberMap = new HashMap<>();
    // Loop through array and mark occurring items
    for (int i : array) {
        // If key exists, it is a duplicate
        if (numberMap.containsKey(i)) {
            numberMap.put(i, numberMap.get(i) + 1);
        } else {
            numberMap.put(i, 1);
        }
    }

    for (Integer key : numberMap.keySet()) {
        // anything with more than one occurrence is a duplicate
        if (numberMap.get(key) > 1) {
            System.out.println(key + " is a reoccurring number that occurs " + numberMap.get(key) + " times");
        }
    }
}

Assuming that the code is added to ArrayTest class, you could all it like this.

public class ArrayTest {
    public static void main(String[] args) {
        int array[] = {32,3,3,4,5,6,88,98,9,9,9,9,9,9,1,2,3,4,5,6,4,3,7,7,8,8,88,88};
        ArrayTest test = new ArrayTest();
        test.printDuplicates(array);
    }
}

If you want to change the code above to look for numbers that reoccur exactly twice (not more than once), you can change the following code

if (numberMap.get(key) > 1) to if (numberMap.get(key) == 2)

Note: this solution takes O(n) memory, so if memory is an issue, Ian's solution above would be the right approach (using a nested loop).

J. Lee
  • 513
  • 4
  • 15
0

The problem statement is not clear, but lets assume you can't sort (otherwise the problem greatly simplifies). Lets also assume the space complexity is constrained, and you can't keep a Map, etc, for counting the frequency.

You can use use lookbehind, but this unnecessarily increases the time complexity.

I think a reasonable approach is to reserve the value -1 to indicate that an array position has been processed. As you process the array, you update each active value with -1. For example, if the first element is 32, then you scan the array for any value 32, and replace with -1. The time complexity does not exceed O(n^2).

This leaves the awkcase case where -1 is an actual value. It would be required to do a O(n) scan for -1 prior to the main code.

If the array must be preserved, then clone it prior to processing. The O(n^2) loop is:

for (int i = 0; i < array.length - 1; i++) {
    boolean multiple = false;
    for (int j = i + 1; j < array.length && array[i] != -1; j++) {
        if (array[i] == array[j]) {
            multiple = true;
            array[j] = -1;
        }
    }
    if (multiple)
        System.out.println("element occuring multiple times is:" + array[i]);
}
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
0

What you can do, is use a data structure that only contains unique values, Set. In this case we use a HashSet to store all the duplicates. Then you check if the Set contains your value at index i, if it does not then we loop through the array to try and find a duplicate. If the Set contains that number already, we know it's been found before and we skip the second for loop.

int array[] = {32,3,3,4,5,6,88,98,9,9,9,9,9,9,1,2,3,4,5,6,4,3,7,7,8,8,88,88};
HashSet<Integer> duplicates = new HashSet<>();
for(int i= 0;i<array.length-1;i++)
{
    if(!duplicates.contains(array[i]))
        for(int j=i+1;j<array.length;j++)
        {
            if((array[i])==(array[j]) && (i != j)){
                duplicates.add(array[i]);
                    break;
            }
        }
}
System.out.println(duplicates.toString());

Outputs

[3, 4, 5, 6, 7, 88, 8, 9]
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0
    // print duplicates
    StringBuilder sb = new StringBuilder();
    int[] arr = {1, 2, 3, 4, 5, 6, 7, 2, 3, 4};
    int l = arr.length;
    for (int i = 0; i < l; i++)
    {
        for (int j = i + 1; j < l; j++)
        {
            if (arr[i] == arr[j])
            {
                sb.append(arr[i] + " ");
            }
        }
    }
    System.out.println(sb);
Sandeep Duve
  • 131
  • 1
  • 5
-1

Sort the array. Look at the one ahead to see if it is duplicate. Also look at one behind to see if this was already counted as duplicate (except when i == 0, do not look back).

import java.util.Arrays;

public class ArrayTest{

    public static void main(String[] args) {

        int array[] = {32,32,3,3,4,5,6,88,98,9,9,9,9,9,9,1,2,3,4,5,6,4,3,7,7,8,8,88,88};

        Arrays.sort(array);
        for(int i= 0;i<array.length-1;i++){
            if((array[i])==(array[i+1]) && (i == 0 || (array[i]) != (array[i-1]))){
                System.out.println("element occuring twice are:" + array[i]);
            }
        }
    }
}

prints:

element occuring twice are:3
element occuring twice are:4
element occuring twice are:5
element occuring twice are:6
element occuring twice are:7
element occuring twice are:8
element occuring twice are:9
element occuring twice are:32
element occuring twice are:88
Ari Singh
  • 1,228
  • 7
  • 12