0

Im trying to check which elements are the same in an array and then return the elements that are the same. Im thinking that i have to do a for loop inside of a for loop but im not sure. This is what i have so far:

for (int p = 0 ; p < temperatures.length ; p++) {
    for (int j = 0 ; j < temperatures.length ; j++) {
        if (temperatures[p] == temperatures[j]) {
            System.out.println("matching" + j + p);
        }
    }
}

How i created the array:

for(int i = 0; i < temperatures.length; i++) {
    System.out.println("Please enter the temperature in Celcius for day " + (i+1));
    temperatures[i] = new Data(input.nextDouble());
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
destroted
  • 51
  • 1
  • 9
  • 1
    What is the problem you encounter? – Jeroen Vannevel Oct 10 '14 at 21:24
  • if temperatures are double or float, maybe you should compare each other using some delta – Leo Oct 10 '14 at 21:25
  • Is this a homework, or something like that? If not, see [this](http://stackoverflow.com/questions/14260134/elegant-way-of-counting-occurrences-in-a-java-collection) SO question. – Nagy Vilmos Oct 10 '14 at 21:27
  • When i putt in input for the 7 temperatures it says theres 7 matches because i think its matching each element to itself – destroted Oct 10 '14 at 21:27
  • Hey, I think you can find useful the answer presented here. I think it is very close to what you want to do and it is well explained. Regards and happy coding :). URL: http://stackoverflow.com/questions/3951547/java-array-finding-duplicates – Marcelo Tataje Oct 10 '14 at 21:29

2 Answers2

0

The reason why it says 7 matches is due to

for(int j = 0; j < temperatures.length; j++)
            {
                if(temperatures[p] == temperatures[j]) // This will have te..[0]==te..[0] .... te..[1]==te..[1] .... te..[6]==te..[6]

You should change it to

for(int j = p+1; j < temperatures.length; j++)
            {
                if(temperatures[p] == temperatures[j]) //Here there's no chance of p==j. So this will work.
user562
  • 88
  • 1
  • 11
  • I did what you suggested, I am getting 0 matches when i putt all of the temperatures the same?> – destroted Oct 10 '14 at 21:43
  • I've updated my code int j= i+1; to int j=p+1; As p is the variable which you're declaring in the first for loop, by running this code in my Eclipse, I'm getting correct output. – user562 Oct 10 '14 at 21:54
0

Another approach is to first sort the array (as long as they are number or can be sorted). Then checking for duplicates is trivial.

Sorting with QuickSort or TimSort is O(n log n) average, and you can tell duplicates in O(n). If you cannot modify the original then you will need an extra O(n) in space and time (to copy).

Note that Arrays.sort(double[]) is O(n log n).

This algorithm is O(2n log n) which is O(n log n) = O(log n!).

Simple example with random ints.

        int[] d = ThreadLocalRandom.current().ints(100, 0, 100).toArray();
    Arrays.sort(d); // O(n log n)

    boolean dup = false;
    for (int i = 1; i < d.length; i++) { // O(n)
        if (d[i - 1] == d[i]) {
            dup = true;
        } else {
            if (dup) {
                System.out.print(d[i - 1] + " ");
            }
            dup = false;
        }
    }
    System.out.println("");

    for (int i = 0; i < d.length; i++) {
        System.out.print(d[i] + " ");
    }
ssedano
  • 8,322
  • 9
  • 60
  • 98