Fluffy duck
Fluffy duck

Reputation: 13

Counting and writing duplicates

I'm doing some homework and I'm kinda stuck on 2nd part of homework. The first part of homework was to make 2 arrays

  1. one array that will show all positive numbers
  2. one array that shows all negative numbers.

The second part of the assignment is to count and write which numbers are duplicates, no need to write how many times they are appearing only which numbers. And code must be working for any array not just this array in the example.

Thanks in advance.

     package ass1;

    import java.util.Arrays;


    public static void main(String[] args) {
    int []array= {12,23,-22,0,43,545,-4,-55,43,12,0,-999,-87};
    int positive= 0; 
    int negative=0 ;
    for(int i : array){
        if(i>0)
            positive++;
        else if(i<0)
            negative++;
    }
    int[] neg = new int[negative];
    int[] pos = new int[positive];
    int n = 0;
    int p = 0;
    for (int i=0;i<array.length;i++){
        if(array [i]<0){
            neg[n] = array[i];
            n++;
        }
        else if(array[i]>0){
            pos[p] = array [i];
                    p++;}

    } 
    System.out.println("Positive numbers : "+Arrays.toString(pos));
    System.out.println("Negative numbers : "+Arrays.toString(neg));
    Arrays.sort(array);


    }



  }

Upvotes: 0

Views: 50

Answers (1)

Here there the code for your problem, I put some comments for help you to understand the code.

import java.util.*;

public class Homework {

    public static void main(String[] args) {
        List<Integer> array = Arrays.asList(12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87);

        List<Integer> positive = new ArrayList<>();
        List<Integer> negative = new ArrayList<>();
        List<Duplicate> duplicates = new ArrayList<>();

        array.forEach(item -> {
            //get the positive and negative numbers
            if (item < 0)
                negative.add(item);
            else
                positive.add(item);

            //get the amount of times that number appeared in the array
            int frequency = Collections.frequency(array, item);
            //check if the number is not already into duplicates array
            if (frequency > 1 && !duplicates.contains(new Duplicate(item))) {
                duplicates.add(new Duplicate(item, frequency));
            }
        });

        //print the result
        positive.forEach(item -> System.out.printf("Positive value %d %n", item));
        negative.forEach(item -> System.out.printf("Negative value %d %n", item));
        duplicates.forEach(item -> System.out.printf("Duplicate value %d amountOfDuplications %d %n",
                item.getNumber(), item.getAmountOfDuplications()));
    }
}

/**
 * This class helps us to store the number and the amountOfDuplications in the same array
 */
class Duplicate {
    private int number;
    private int amountOfDuplications;

    public Duplicate(int number) {
        this.number = number;
    }

    public Duplicate(int number, int amountOfDuplications) {
        this.number = number;
        this.amountOfDuplications = amountOfDuplications;
    }

    public int getNumber() {
        return number;
    }

    public int getAmountOfDuplications() {
        return amountOfDuplications;
    }

    //here in the equals and hashcode I used only the number as a key,
    // because I considered a duplicate two objects with the same "number" field
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Duplicate)) return false;
        Duplicate duplicate = (Duplicate) o;
        return getNumber() == duplicate.getNumber();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getNumber());
    }
}

Here you will find some links with the java documentation:

Collections.frequency()

array.forEach() and array.contains()

Equals Method

HashCode Method

Upvotes: 1

Related Questions