0

I know the title is really bad but I spent like 10 minutes thinking of a way to describe my problem in a concise way and couldn't. This program is supposed to create a numUnique() static method that returns the amount of unique numbers in an array. So, for example, if I have an array with {2, 4, 2, 7, 16, 4} the amount of unique numbers would be 4 (2, 4, 7 and 16).

I was writing the code to find the duplicates in my array, then I realized that I didn't know what to do with it when I had the duplicates, and I've been breaking my head trying to think of a solution but I can't.

Here is the code so far:

public class Unique {
    public static void main(String[] args) {
        int[] numbers = {1, 6, 2, 14, 6, 8, 2, 1, 23};
        numUnique(numbers);
    }

    public static void numUnique(int[] array){
        Arrays.sort(array);
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] == array[j])
                    //code here
            }
        }
    }
}

4 Answers4

0

My understanding is you to count (not return them all) the number of unique elements in the array. You are attempting to look all the way down, but it's enough to track the number of duplicates as you go. This code looks back to see if the current entry duplicates the previuos one (which works because you sorted, which was a good step).

import java.util.Arrays;
public class Unique {
public static void main(String[] args) {
    int[] numbers = {
        1,
        6,
        2,
        14,
        6,
        8,
        2,
        1,
        23
    };
    System.out.println(numUnique(numbers));
}

public static int numUnique(int[] array) {
    Arrays.sort(array);
    int dups = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i] == array[i - 1]) {
            dups++;
        }
    }
    return (array.length - dups);
}
}
Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23
  • Ok, I did that and it worked for the first array, but then I tested with another array with {7, 7, 7, 7, 7, 7} and instead of returning 0 it returned -9, since it's the arrays length - dups. Any idea on how I fix it? –  May 23 '19 at 01:00
  • At https://www.jdoodle.com/online-java-compiler I just ran it on {7, 7, 7, 7, 7, 7} and it returned 1. The array's length is 6 and dups is 5. – Jeremy Kahan May 23 '19 at 01:49
  • oh thank you so much! i didn't copy and paste your code, only modified mine but i f-d up somehow, thank u thank u thank uu –  May 23 '19 at 03:05
  • the issue with yours was that inner for loop (see my comment below to Mikosz). Happy to help. – Jeremy Kahan May 23 '19 at 03:08
0

If you want to keep the code that you have so far and sort the list, there are two options that seem to make sense. Where one is to return the number of unique integers, and the other is to return a list of the unique integers.

Returning the number of unique numbers:

public static int numUnique(int[] array) {
    ArrayList<Integer> uniques = new ArrayList<>();
    Arrays.sort(array);

    uniques.add(array[0]);

    int prev = array[0];
    for (int i = 1; i < array.length; i++) {
        if(array[i] != prev){
            uniques.add(array[i]);
            prev = array[i]
        }
    }
    return uniques.size();
}

Returning the list of unique numbers, would simply require you to return the uniques list, without the .size().

If you want a array instead of a list, you will need to return the following:

return uniques.toArray(new Integer[list.size()]);
0

If you're using Arrays why not explore it further

private static long numUnique(int[] numbers) {
    return Arrays.stream(numbers).distinct().count();
}
Cwrwhaf
  • 324
  • 3
  • 5
0

Keeping your code as it is:

If You want to know how many non-duplicates are in you array:

public static void numUnique(int[] array) {
        Arrays.sort(array);
// Here, make a variable that keeps size of an array
        int arrayLenght = array.length;
// Here is our counter:
        int duplicates = 0;

        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] == array[j])
                    // when you find duplicate, add 1 to our counter:
                    duplicates++;

            }
        }
        //at the end you can count how many non-duplicates are in the array:
        arrayLenght -= duplicates;
        System.out.println("There are " + arrayLenght + " non duplicates in our array.");
    }

Edit This solution do not work in every situation.

I know this is not the most optimal way to do this, but the only i know hah:

public static void numUnique(int[] array) {
        Arrays.sort(array);

        int arrayLenght = array.length;
        int duplicates = 0;

        for (int i = 0; i < array.length; i++) {
//a little change in our second for-loop:
            for (int j = 0; j < array.length; j++) {
                if (array[i] == array[j]) { 
//Here i'm just summing up the duplicates
                duplicates ++;
                }

            }
// Aaand here, on a console, i print the value (array[i]) and how many duplicates does it have.

//to be exact, if the duplcicates rise only once so it's equal to 1, it means that there is no duplicate of this value (it found itself in the loop).

            if (duplicates == 1) {
                duplicates = 0;
            }

            System.out.println(array[i] + " have " + duplicates + "duplicates");

// And what is imporatnt We have to reset our duplication-container!
// so in further loops it will count duplicates for each value in array

            duplicates = 0;

        }

    }

If you find another issue, ask me right away

Mikosz
  • 27
  • 7
  • hey, thank you! i did just that, but when i tried the method with different arrays the outputs were definitely wrong. i understand why they're wrong but couldn't figure out how to fix them. first array: {2,3,2,2} output: 1 –  May 23 '19 at 01:32
  • and second array: {7,7,7,7,7,7} output: -9 –  May 23 '19 at 01:32
  • The issue with the first approach was the second for loop. In the 7's cases, you're counting the last 5 7's as duplicating the first one, then the last 4 as duplicating the second ... the last one as duplicating the 5th. 5+4+3+2+1=15. 6-15 = -9 – Jeremy Kahan May 23 '19 at 02:06