-2

I'm pretty new to programming in Java and I want to make a program that will print out some values from a file. I want to import a array list from a file which contains a large set of repeated numbers. The program should print out only one unique number of set.

For example, the array contains these numbers:

0,0,0,0,2,2,2,2,2,3,3,3,3,3,5,5,5,5,8,8,10,10,2,2,2,3,3,7,7

and what I should get out of it is this:

0,2,3,5,8,10,2,3,7

The same would be needed if the array wasn't containing integers, but floating point numbers:

0.23, 0.23, 0.23, 0.23, 1.89, 1.89, 1.89, 1.89, 1.89, 2.56, 2.56, 2.56, 2.56, 2.56, 3.13, 3.13

The output should be:

0.23, 1.89, 2.56, 3.13

Has anyone some suggestions on how should I build the loop?

Thanks in advance

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Ivn Bubrov
  • 43
  • 1
  • 8

3 Answers3

1

It seems like you want to prevent non-continuous repetition. I would start by setting a variable to the first value in the array, output that value, then iterate the array until I encountered a new unique value. I would output a comma and the new value (storing it in the value variable) and then continue the loop (until I reached the end of the array). That is, something like -

public static void main(String[] args) {
    int[] arr = { 0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8,
            8, 10, 10, 2, 2, 2, 3, 3, 7, 7 };
    int v = arr[0];
    System.out.print(v);
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] != v) {
            v = arr[i];
            System.out.printf(", %d", v);
        }
    }
    System.out.println();
}

Output is the requested

0, 2, 3, 5, 8, 10, 2, 3, 7
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Exactly this! But, how would you modify the int[] arr={} part to input a list of numbers from a text file, without knowing it's length? – Ivn Bubrov Sep 22 '14 at 00:48
  • @IvnBubrov Three Options. 1. Use an `ArrayList` to dynamically get it in one pass, or 2. Use two passes and count the number of elements in the first pass, or 3. Store the number of elements in the file. – Elliott Frisch Sep 22 '14 at 00:50
1

Code:

    int[] input = new int[]{0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 10, 10, 2, 2, 2, 3, 3, 7, 7};
    int current = input[0];
    boolean found = false;

    for (int i = 1; i < input.length; i++) {
        if (current == input[i] && !found) {
            found = true;
        } else if (current != input[i]) {
            System.out.print(" " + current);
            current = input[i];
            found = false;
        }
    }
    System.out.print(" " + current);

output:

0 2 3 5 8 10 2 3 

Explanation:

you set current with first value of array after that you go through the loop.Inside the loop, if the current value equal to each elements and flag fount is false which mean duplicate have been not seen and you it has been seen right now, so flag which is found variable has been set to true which shows duplicate has been found or seen an do nothing.

For the else part, if the element of array not equal to current variable means there is not duplicate, and right current variable that contains the before seen duplicate at the console. Update the variable current with new duplicate and set flag found to false which shows have not seen new seen duplicate. Keep doing this and right last duplicate variable when you are done traversing the loop.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • Than you very much! I really appreciate your explanation. I had to edit the question for additional information. What would you change if the data contains floating point numbers instead of integers? – Ivn Bubrov Sep 22 '14 at 01:11
  • the process is the same. just you need to change the type of array to double and current variable to double as well :) – Kick Buttowski Sep 22 '14 at 01:12
  • @IvnBubrov how did it go? – Kick Buttowski Sep 22 '14 at 01:27
  • It works perfect!!! :) Thanks a lot! Now, I am going to try to learn how to import a list of numbers from a text file and the program will do everything I need. – Ivn Bubrov Sep 22 '14 at 01:30
0

Easiest solution is to create temporary LinkedHashSet and put all your values there.

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

...

public static void main(String[] args) {
    Set<Integer> temp = new LinkedHashSet<Integer>(Arrays.asList(0, 0, 0, 0, 2, 2, 2, 2, 2, 
         3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 10, 10, 2, 2, 2, 3, 3, 7, 7));
    System.out.print(temp);
}

Please note that number "2" should be in a list just once. Correct your question sample, please.

rsming
  • 16
  • 1
  • 1
    I am not sure I could use sets here, because the repeated numbers after some time are needed, so the 2 is correctly written, and the output should've been: 0,2,3,5,8,10,2,3,7 – Ivn Bubrov Sep 22 '14 at 15:23