1

By mode, I am checking if the array has duplicates. I checked similar queries down here but they addressed the question with answers containing ArrayList and HashMap. I am not familiar with them and trying to answer it with an array. My following code only works for 1 duplicate and unable to perform multiple duplicate detection.

public class Mode {

    public static void main(String[] args) {
        int[] num = {2,3,4,5,8,8,8,7,7,7};
        int mode = mode(num);
        System.out.println(mode);
    }

    public static int mode(int[] num){
        for(int x=0; x < num.length; x++){
            for(int y=x+1; y < num.length; y++){
                if(num[x] == num[y]){
                    return num[x]; 
                }
            }
        }
        return num[0]; 
    }
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
kar
  • 4,791
  • 12
  • 49
  • 74
  • 1
    It exits because after the first duplicate it returns. Line return num[x] instead you should store the duplicate number in an array or store it in a string and return the the array or string – Matt Penna Nov 27 '13 at 15:14
  • Check the following post http://stackoverflow.com/questions/3951547/java-array-finding-duplicates/3951647#3951647. There are a lot of answers proposed. – istovatis Nov 27 '13 at 15:20
  • Your return type is `int` do you want the number of duplicates returned? Or do you want to know which of the elements in the list are duplicates? – Sanchit Nov 27 '13 at 15:27

3 Answers3

1

The method mode should return a HashSet that contains the duplicates and not an int. Also note that your inner loop is incorrect. You should do:

public static HashSet<Integer> mode(int[] num){
    HashSet<Integer> dup = new HashSet<>();
    for(int x=0; x < num.length; x++) {
       for(int y=0; y < num.length; y++) {
          if(num[x] == num[y] && x != y) {
              dup.add(num[x]); 
          }
       }
    }
    return dup;
}

This solution is O(n2). You can achieve a better solution if you:

  • Sort the array.
  • Travel on it (only one time), if num[i] == num[i + 1], it's a duplicate.

This solution is O(n*log(n)) - Sorting and traveling on the array only once.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Your code outputs correctly but I am working on understanding HashSet. Wondering if there is another way to go about it without the use of ArrayList and HashSet. – kar Nov 27 '13 at 15:36
  • @user2840178 Yes there is. See my suggestion, it's a better performance solution. – Maroun Nov 27 '13 at 15:39
0

If you want a list of all of the duplicates, this code will give you the answer. If you want just the numbers that are duplicated use the code from @Stefan.

import java.util.Arrays;

public class Mode {

  public static void main(String[] args) {
    int[] num = {2, 3, 4, 5, 8, 8, 8, 7, 7, 7};
    int[] mode = mode(num);
    int i;
    System.out.print("The duplicates are: ");
    for (i = 0; i < mode.length - 1; i++) {
      System.out.print(mode[i] + ", ");
    }
    System.out.println(mode[i] + ".");
  }

  public static int[] mode(int[] num) {
    int numberOfDuplicates = 0;
    int[] duplicates = new int[num.length];
    Arrays.sort(num);
    for (int i = 1; i < num.length; i++) {
      if (num[i - 1] == num[i]) {
        duplicates[numberOfDuplicates++] = num[i];
      }
    }
    return Arrays.copyOf(duplicates, numberOfDuplicates);
  }
}
Richard Povinelli
  • 1,419
  • 1
  • 14
  • 28
-1
  1. You need to return HashSet OR LinkedHashSet for mode method instead of single int.
  2. The first for statement is INCORRECT. It must be

    for (int x=0; x < num.length - 1; x++)  //--- You used x < num.length
    
  3. The source code can be like below

    Set<Integer> result = new HashSet<Integer>();
    for(int x=0; x < num.length - 1; x++) {
        boolean duplicated = false;
        for(int y=x+1; y < num.length; y++) {
            if(num[x] == num[y]) {
                duplicated = true;
                break;  
            }
        }
        if (duplicated) result.add(num[x]);
    }
    return result;
    
LHA
  • 9,398
  • 8
  • 46
  • 85
  • You don't need boolean to do what you describe here. you even don't need hashset in your solution. Imho this is not a solution and code is oversized. – Stefan Nov 27 '13 at 15:50
  • I don't agree with your ideas. Using Set or Array or even other types depending on what we want. I used duplicated variable here is to improve performance. The time will be less than O(n2) as @Maroun Maroun solution. – LHA Nov 27 '13 at 16:14
  • Though your Complexity mention may be correct it is useless to use a Set because you break the loop on the first duplicate and only input one value into the HashSet. Therefor you don't need HashSet, a simple Integer can do the same. – Stefan Nov 28 '13 at 08:37
  • I used hashset because I thought you do not need return duplicate values. You can use List instead using Set and removing duplicated variable. It is very simple change. If you don't care about performance or your array is not quite big. This is a simple solution for you. I think. – LHA Nov 29 '13 at 15:45