jhm
jhm

Reputation: 27

Single occurrences in int [] Array

I want that the amount of occurrences of single numbers is getting printed out. But the problem is that just the number that is defined by count is getting printed out.

public static void main(String[] args) {
    int[] numbers = {10,11,9,5,5,3,2,2,1};
    int nt = countSingles(numbers);
    System.out.println(nt);
}

public static int countSingles(int[]numbers) {
    if(numbers == null)return -1;
    if(numbers.length<=0)return -1;
    int count =1;
    for(int i=0; i<numbers.length-1;i++) {
        if(numbers[i]<numbers[i++])return -1;
        if(numbers[i]>numbers[i++]&&numbers[i++]>numbers[i+2]) {
            count++;
        }
    }

    return count;
}

Upvotes: 2

Views: 381

Answers (4)

dehasi
dehasi

Reputation: 2773

The appearance of each number should be counted and stored. i.e. in Map

   int[] numbers = {10, 11, 9, 5, 5, 3, 2, 2, 1};

    Map<Integer, List<Integer>> map = Arrays.stream(numbers)
    .boxed()
    .collect(Collectors.groupingBy(Function.identity(), mapping(identity(), toList())));

Then it will be possible to find single numbers.

  long nt = map.entrySet().stream()
    .filter(e -> e.getValue().size() == 1).count();

Upvotes: 0

Azzabi Haythem
Azzabi Haythem

Reputation: 2413

You can use java 8 and stream and FILTER :

   public static void main(String[] args) {
        Integer[] numbers = {10,11,9,5,5,3,2,2,1};
        Long nt = countSingles(numbers);
        System.out.println(nt);

    }

    public static Long countSingles(Integer[]numbers) {
        List<Integer> list =  Arrays.asList(numbers);        
        return  list.stream().filter(i->isSingle(i,list)).count();
    }

    static boolean isSingle(int i, List<Integer> list ){
        int occurence=0;
            for (Integer aInteger:list) {
                if(aInteger==i){
                    occurence++;
                    if(occurence>1){
                        return false;
                    }
                }
            }
        return true;
    }

Upvotes: 1

Jayanth
Jayanth

Reputation: 816

You can try the below code: It works too, i guess quite efficient as we will be using HashMap

import java.util.HashMap;


public class arrayTest {
    public static void main(String[] args) {
        int[] numbers = {10,11,9,5,5,3,2,2,1};
        System.out.println(countSingles(numbers));

    }

public static HashMap<Integer, Integer> countSingles(int[]numbers) {

HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();

for(int i=0; i<numbers.length-1;i++) {
    if(hash.get(numbers[i])!= null){
       int num = hash.get(numbers[i]);
       num++;
       hash.put(numbers[i], num);
    }else{
        hash.put(numbers[i], 1);
    }
}

return hash;
}
}

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

Below is a very basic approach. There are MANY ways of doing this. My approach was meant to be easy to understand and follow.

int[] numbers = {10,11,9,5,5,3,2,2,1};

public static int countSingles(int[] numbers) {
    // Avoid NPE
    if (numbers == null) {
        return -1;
    }

    int count = 0;

    // Loop the numbers once
    for (int i = 0; i < numbers.length; i++) {
        // Loop the numbers one more time, comparing each of them 
        // all the other numbers in the array
        for (int j = 0; j < numbers.length; j++) {

            // Do not compare the same number to itself, as these 
            // will always be equal (and is not really what we want to do)
            if (i == j) {
                continue;
            }

            // If the two numbers are equal, continue to the next number, 
            // this one is not a single occurence
            if (numbers[i] == numbers[j]) {
                break;
            }

            // If the value (index) of j equals the length of the array, 
            // and we did not break on the previous if, we know
            // that the current (outer) number is a single occurence, 
            // increase the count
            if (j == (numbers.length - 1)) {
                count++;
            }
        }
    }

    // Return the number of single occurences
    return count;
}

Upvotes: 1

Related Questions