Number of occurrences in array


You have declared array of positive ints and given distance. Find how many pairs there is with given distance. Ommit repeating for occurrence of many same numbers!

package com.pdb.tst;

import java.util.Arrays;

public class Distance {

    public static int distance(int a[], int k) {
        int result = 0;
        Arrays.sort(a);
        stringify(a);
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) { if (a[j] - a[i] == k) { if (i > 1) {
                        if (a[i] - a[i - 1] != 0) {
                            result++;
                        }
                    } else {
                        result++;
                    }
                }
            }
        }

//        System.out.println("Number of occurrences pairs with defined distance without repeating : " + result);
        return result;
    }

    private static void main(String[] args) {
        //[0 2 123 125 421 457 1234 1234 1236 3121 675878 ]
        int a[] = {1234, 421, 424, 2, 675878, 123, 1234, 125, 3121, 1236, 0};
        int k = 0;

        stringify(a);
        distance(a, k);
    }

    private static void stringify(int a[]) {
        System.out.print("[");
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println("]");
    }
}

Update:
And some basic junit tests to this solution!

package com.pdb.tst;

import junit.framework.TestCase;
import org.junit.Test;

/**
 * Created by Padob on 2016-02-21.
 */
public class DistanceTest extends TestCase {
    int a[];
    int k;

    @Override
    protected void setUp() throws Exception {
    }

    @Override
    protected void tearDown() throws Exception {
        a = null;
        k = 0;
    }

    @Test
    public void testZeroDistance() {
        //given
        a = new int[]{2, 4, 76, 2, 4, 8};
        k = 0;

        //when
        int pairsNr = Distance.distance(a, k);

        //then
        assertEquals(2, pairsNr);
    }

    @Test
    public void testOneDistance() {
        //given
        a = new int[]{1, 4, 2, 3, 6};
        k = 1;

        //when
        int pairsNr = Distance.distance(a, k);

        //then
        assertEquals(3, pairsNr);
    }

    @Test
    public void testDistanceWithRepeatedValues() {
        //given
        a = new int[]{123, 125, 127, 125, 128};
        k = 2;

        //when
        int pairsNr = Distance.distance(a, k);

        //then
        assertEquals(3, pairsNr);
    }
}

Tagi: , ,
Paweł Dobrzański

Start-up's fan, technological conferences member, social media enthusiast and low-cost trips traveler.

You may also like

Zobacz mój kurs online!

mistrz cv okladka

https://mistrzcv.pl/

Więcej niż na blogu

Cześć!

Witaj na moim blogu! Znajdziesz tu proces budowania mojej aplikacji EventSearch za pomocą jhipstera. Podzielę się z Tobą niuansami dot. zakładania i prowadzenia firmy. Mam również nadzieję, że pomogę Ci zaoszczędzić trochę pieniędzy. Zapraszam!

Dołącz do newslettera!

Najpopularniejsze posty

Kącik promocji!

Udemy.com Home page 125x125

Dzięki, że wpadłeś!

Paweł Dobrzański

Paweł Dobrzański

Start-up's fan, technological conferences member, social media enthusiast and low-cost trips traveler.