4

I have an integer arraylist that has consecutive duplicate lines. I want to output each number that is not the same as the one before.

Sample: 1 3 3 3 2 2 1 2 2 3 3 3

Desired output: 1 3 2 1 2 3

How should I do this?

I am essentially creating an inverted index of words to any input of text files. I have a file/buffered reader reading from multiple text files line by line and placing each line into a 2d array with the first column being a .split word and the second column being the filename from which the word was obtained. I then have a for loop to get the frequency a particular word occurs in all of the files and add it to another column. I then copied each column into their own arraylist. I added the words arraylist to a hashset to remove the duplicates. but I want to remove only consecutive duplicates of the frequency arraylist.

2
  • 1
    What have you already written? Commented Feb 12, 2015 at 1:11
  • 2
    Questions should include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Please edit your question and include your attempt to solve this problem. More info here Commented Feb 12, 2015 at 1:14

3 Answers 3

7

Possible idea comparing elements to each other:

public static ArrayList<Integer> noConsecutiveDups(ArrayList<Integer> input) {

  ArrayList<Integer> newList = new ArrayList<Integer>();

  // Always add first value
  newList.add(input.get(0));

  // Iterate the remaining values
  for(int i = 1; i < input.size(); i++) {
    // Compare current value to previous
    if(input.get(i-1) != input.get(i)) {
       newList.add(input.get(i));
    }
  }

  return newList;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You're a miracle worker. i kept trying to compare input.get(i) with input.get(i+1) and it kept going out of bounds. I didn't think to add one and then compare to the previous.
LOL! Just being creative. :-)
1

I saw two answers here, one was using extra space and other was using extra time(~n^2) so here's a simple reverse iteration and removal that would help to remove consecutive duplicates in o(n) time without using extra space:

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        list.add("a");
        System.out.println("Before:"+list);
        
        for(int i =list.size()-1;i>=1;i--){
            if(list.get(i).equals(list.get(i-1))){
                list.remove(i);
            }
        }
        System.out.println("After:"+list);
     }
}

Comments

-1

The above logic is by taking a new list and making changes.But the below code helps you to make changes for the same list.

public class AvoidConsecutiveDups {
    public static void main(String args[]) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("a");
        list.add("a");

        for (int i = 1; i <= list.size() - 1; i++) {
            if (list.get(i - 1) == list.get(i)) {
                list.remove(i - 1);
                for (int j = 1; j <= list.size() - 1; j++) {
                    if (list.get(j - 1) == list.get(j)) {
                        list.remove(j - 1);
                    }
                }
            }
        }

        Iterator itr = list.iterator();

        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

1 Comment

I don't like the idea of having a loop inside another loop when a simple reverse traversal and removal can do the trick....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.