Question

So there lives an array list:

  ArrayList<Character> charOutput = new ArrayList<Character>();

He eats chars, so he ends up like this:

  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, *, *, *, *, *, *, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

But this is quite grotesque in our particular applicaiton. we want to trim him down, such that, finally, he looks like this:

 [0,1,*,3,4]

So, how to do that?

Was it helpful?

Solution 2

scan linearly through the array keeping track of the current item. If the next item matches the current item ignore it and move forward. If it did not match, then make the new item as the current item and output the current item.

And here is the code

 public static void main(String []args)
 {
    ArrayList<Character> charOutput = new ArrayList<Character>();
    ArrayList<Character> charOutputTrimmed = new ArrayList<Character>();
    charOutput.add('0');charOutput.add('0');charOutput.add('0');
    charOutput.add('1');charOutput.add('1');charOutput.add('1');
    charOutput.add('*');charOutput.add('*');charOutput.add('2');
    charOutput.add('2');charOutput.add('0');

    Character currentChar = charOutput.get(0);
    charOutputTrimmed.add(currentChar);
    for(int i=1;i<charOutput.size();i++)
    {
        if(currentChar != charOutput.get(i))
        {
            currentChar = charOutput.get(i);
            charOutputTrimmed.add(currentChar);
        }
    }

    for(int i=0;i<charOutputTrimmed.size();i++)
    {
        System.out.print(charOutputTrimmed.get(i) + "\t");
    }
 }

OTHER TIPS

Construct a Set from the ArrayList then convert it back to ArrayList.

If you don't want to use collections, you can simply iterate on the given ArrayList and for each number you see, add it to another ArrayList as long as it wasn't added before.


Edit: (Not a full solution with code)

If you want to remove only contiguous elements, iterate on the ArrayList, add the current element. Now as long as the next element is equal to the current element, proceed.. When you see another element that's different from the current one, add it..

Use Set to get unique list.Good thing to do this is, It works for any user defined objects..

//Converting ArrayList to HashSet to remove duplicates
LinkedHashSet<Character> listToSet = new LinkedHashSet<Character>(duplicateList);

//Creating Arraylist without duplicate values
List<Character> listWithoutDuplicates = new ArrayList<Character>(listToSet);

loop around and just keep the non repating chars

ArrayList<Character> charInput = new ArrayList<Character>();
ArrayList<Character> charOutput = new ArrayList<Character>();

char lastch = 255;
for (char ch : charInput) {
    if (ch != lastch) {
        charOutput.add(ch);
    }
    lastch = ch;
}

Pass the arraylist object to LinkedHashSet you will get the required output.

Set<Character> s = new LinkedHashSet<Character>(charOutput);

Thanks and regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top