-2

Given a sequence of numbers like this:

1 2 2 5 5 5 1 7 3 7 7 7

Output should be

1 2 5 1 7 3 7

The current output of my code is

1 2 5 1 7 3

I am unable to fix the problem. Can anyone tell me what should I do or change in my code?

Here's my current code:

public class Q3 {
    public static void main(String args[]) {
        int [] input=new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7};
        int current= input[0];
        boolean found=false;
        for(int i=0; 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;
             }
        }    
    }
}
jww
  • 97,681
  • 90
  • 411
  • 885
farhana konka
  • 81
  • 1
  • 2
  • 8
  • Welcome to SO. Can you be more specific about the question. – arunmoezhi Oct 03 '14 at 05:21
  • Print the first occurrence and ignore the rest... – MadProgrammer Oct 03 '14 at 05:22
  • @RuchiraGayanRanaweera. How does the question look after the edit done by MadProgrammer – arunmoezhi Oct 03 '14 at 05:25
  • anyone can believe this is my answer :D haha.... – Kick Buttowski Oct 03 '14 at 05:26
  • @MadProgrammer the op stole my answer how can I report it? – Kick Buttowski Oct 03 '14 at 05:34
  • @KickButtowski don't know what you meant by your offensive comment, but this is stated in SO rules when you accept become a user of this site. Instead of taking this as *plagiarism*, be happy for someone in another part of the world using your code (with problems, but still yours). – Luiggi Mendoza Oct 03 '14 at 05:42
  • @farhanakonka FYI- If you've picked up some from SO or even another resource which you can't get running, it's generally consider good manners to reference the source – MadProgrammer Oct 03 '14 at 05:44
  • @KickButtowski you can find the specific statement of this at the bottom of any page in the site, it is stated like this: *user contributions licensed under [cc by-sa 3.0](http://creativecommons.org/licenses/by-sa/3.0/) with [attribution required](http://blog.stackoverflow.com/2009/06/attribution-required/)* – Luiggi Mendoza Oct 03 '14 at 05:46
  • @LuiggiMendoza :) +1 for Unicorns. – Edward J Beckett Oct 03 '14 at 06:05

5 Answers5

2

The basic problem is, you need to seed the current value with something that is not equal to the first element, this way, you can just loop through the array without issues, flags or other tricks, for example...

int[] input = new int[]{1, 2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7};
int current = -input[0];

for (int i = 0; i < input.length; i++) {
    if (current != input[i]) {
        current = input[i];
        System.out.print(" " + current);
    }
}
System.out.println("");

Which outputs...

1 2 5 1 7 3 7

This sets the current value to the negative of the first element (in this -1), so when we do our first comparison, -1 != 1 and then we can proceeded as normal...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • the op stole my answer how can I report this? – Kick Buttowski Oct 03 '14 at 05:35
  • What do you mean by "stole? – MadProgrammer Oct 03 '14 at 05:36
  • go to the link and see that this is my answer – Kick Buttowski Oct 03 '14 at 05:36
  • 3
    @KickButtowski You can't say OP **stole your answer** SO is open for all to learn something new and he/she just used your answer to learn. – akash Oct 03 '14 at 05:37
  • @TAsk without mentioning me is the plagiarism in my country do not know about yorus – Kick Buttowski Oct 03 '14 at 05:38
  • @KickButtowski If you do not want people to use your code, then why Post it on a public forum? If the OP used your answer as he own answer and did not credit you, then that is a slightly different matter. – Scary Wombat Oct 03 '14 at 05:38
  • @KickButtowski The code you provide here is generally considered "open source", sure it would be nice if the OP had mentioned it, but it wouldn't be considered "stealing" - besides, the OP messed it up anyway ;) – MadProgrammer Oct 03 '14 at 05:39
  • @ScaryWombat this is the rule. if I use others code, I always put source. – Kick Buttowski Oct 03 '14 at 05:39
  • 1
    @KickButtowski It's a common enough algorithm, no offense, have a look around all the answers, did anyone else plagiarize you? Don't get me wrong, it's not a "great" thing, but the code you supply here is provided without license, free for all, that's all were saying. You could have pointed the OP back to the original answer with the single line of correction ;) – MadProgrammer Oct 03 '14 at 05:41
  • @KickButtowski why? you don't have to – danbst Oct 03 '14 at 05:42
  • @danbst what do you mean? – Kick Buttowski Oct 03 '14 at 05:42
  • @KickButtowski "this is the rule. if I use others code, I always put source" -> this is wrong rule. You don't have to put link to original if you use other's code – danbst Oct 03 '14 at 05:44
1

You can try in this way

Eg:

int[] input = new int[]{1, 2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7};
int temp =input[0];
boolean isFirst=true;
for (int i = 0; i < input.length; i++) {
    if(isFirst){
       System.out.print(temp);
       isFirst=false;
    }
    if (temp != input[i]) {
       System.out.print(input[i]);
    }
     temp = input[i];
}

Out put:

 1251737

Logic: I am taking only first occurrence of continuous numbers.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

You can simplify code a lot, if you don't want to remove all duplicates, only closest one.

public class Q3 {
    public static void main(String args[]) {
        int [] input=new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7};
        int base = input[0];
        System.out.print(base);
        for (int current : input) {
            if (current != base) {
                base = current;
                System.out.print(" " + base);
            }
        }    
    }
}

Output:

1 2 5 1 7 3 7
danbst
  • 3,363
  • 18
  • 38
0

This is not right to use other's answer without giving the credit

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

for (int i = 0; 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); <--- you forgot this line 

output:

1 2 5 1 7 3 7

My answer can be found here How to efficiently remove duplicates from an array without using Set

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
0
int [] input = new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7};
int previous = 0;
boolean started = false; // loop is not started yet

for (int i = 0; i < input.length; i++)
{
    // if the loop is not started, or the number is not equal to previously printed number...
    if (!started || input[i] != previous) {
        System.out.print(input[i] + " ");
        previous = input[i];
    }
    started = true;
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98