duplicate number

Go To Last Post
64 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

 

I want to find duplicate number in array I have tried on paper by making flow diagram 

 

Here is my approach to solve the problem 

 

 

I believe I can find only one duplicate number that is 3 and my approach failed when there are more then two duplicate numbers in array 

 

any suggestion would be appreciated  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to do multiple passes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I don't understand your flowchart!

 

What is this for:

What does it mean?

 

Think about how you do this manually: For each element in the array, you need to look at every other element to see if it has the same value.

 

So you need 2 indexes (indices?):

  1. The element you're considering;
  2. The the "other" elements. 

 

 

HINT

 

You only ever need to scan forward from the element you're currently considering ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 31, 2020 - 11:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sky33 wrote:
find duplicate number in array

Actually, you need to clarify that.

 

Do you just need to identify that there is any duplicate ?

 

Do you need to count how many duplicates ?

 

Do you need to list all the duplicated values ?

 

Or what ?!

 

A key part of software development is getting the specification clear, correct, and complete!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
You have to do multiple passes.

 

Do I need to use two FOR loop 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to think about the design before getting bogged down with implementation details like that!

 

Think about what you need to do.

 

This is where the flowchart comes in.

 

Only when you've got that do you need to worry about how to do it.

 

EDIT

 

Usually, the how will become obvious from the what.

 

There is not necessarily going to be just one way to implement the design; ie, there could be multiple possible hows to do your what.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 31, 2020 - 11:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

I don't understand your flowchart!

 

What is this for:

What does it mean?

 

Think about how you do this manually: For each element in the array, you need to look at every other element to see if it has the same value.

 

So you need 2 indexes (indices?):

  1. The element you're considering;
  2. The the "other" elements. 

 

 

HINT

 

You only ever need to scan forward from the element you're currently considering ...

 

I am assuming first number of array is duplicate 

 

awneil wrote:

sky33 wrote:
find duplicate number in array

Actually, you need to clarify that.

 

Do you just need to identify that there is any duplicate ?

 

Do you need to count how many duplicates ?

 

Do you need to list all the duplicated values ?

 

Or what ?!

 

A key part of software development is getting the specification clear, correct, and complete!

Yes first I want find all duplicate numbers in array and then then want to count how many duplicates 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


Seems to me the solution would be something like:

#define N 7

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

int main(void) {
    uint8_t test;
    for (int i = 0; i < N; i++) {
        test = digits[i];
        for (int j = i + 1; j < N; j++) {
            if (digits[j] == test) {
                printf("element %d with value %d is duplicated at element %d\n", i, test, j);
            }
        }
    }
}

When I build and run that I get:

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should know how to do quoting by now.

 

Reminder: https://www.avrfreaks.net/forum/...

 

sky33 wrote:
first I want find all duplicate numbers

So you'll need a way to store multiple values - won't you?

 

sky33 wrote:
then want to count how many

As always, take it one step at a time!

 

But counting them is trivial once you've found them.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed.

 

That saves him having to think or work it out.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
I am assuming first number of array is duplicate 

But you are just using a "Magic Number" of 3.

 

So you are also assuming that the first element of the array is 3.

 

So this won't work for any other array which doesn't have 3 in its first element!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

#define N 7

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

int main(void) {
    uint8_t test;
    for (int i = 0; i < N; i++) {
        test = digits[i];
        for (int j = i + 1; j < N; j++) {
            if (digits[j] == test) {
                printf("element %d with value %d is duplicated at element %d\n", i, test, j);
            }
        }
    }
}

 

3 and 5 are the only duplicate numbers in given array your solution doesn't seems to correct 

 

#include <stdio.h>
#define N 7

int digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

int main(void) {
    int test;
    for (int i = 0; i < N; i++) {
        test = digits[i];
        for (int j = i + 1; j < N; j++) {
            if (digits[j] == test) {
                printf("element %d with value %d is duplicated at element %d\n", i, test, j);
            }
        }
    }
    return 0;
}

Result 

$gcc -o main *.c
$main
element 0 with value 3 is duplicated at element 4
element 1 with value 5 is duplicated at element 6

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

You need to think about the design before getting bogged down with implementation details like that!

 

Think about what you need to do.

 

This is where the flowchart comes in.

 

I have shown my best attempt to solve problem and trying best 

 

this code find duplicate of first number in array

#include <stdio.h>
#define N 7


int main(void) 
{

    int digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

    for (int i = 0; i < N; i++)
    {
       if (digits[0] == 3 )
       {
           printf("duplicate number found : %d \n", digits[i]);
           break;
       }
       else 
       {
         printf("duplicate number not  found \n");
         break;
       }
       
        
    }
    return 0;
}
$gcc -o main *.c
$main
duplicate number found : 3 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
3 and 5 are the only duplicate numbers in given array your solution doesn't seems to correct

Yes it is - look again at the results it prints!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I have shown my best attempt

Well, that was you're first attempt.

 

Just trying to explain to you how to approach this so that you know how to do it yourself - rather than just giving a solution.

 

 

sky33 wrote:
this code find duplicate of first number in array

No, it doesn't.

 

It just checks if element 0 of the array is 3.

 

If it finds that element 0 of the array is 3, it reports that as a duplicate - irrespective of whether 3 is actually duplicated in the array or not!

 

Again, don't jump straight to code - think about what to do first before trying to code how to do it.

 

EDIT

 

Build & Run your code again, but with this data in the array:

int digits[N] = { 3, 5, 4, 2, 0, 9, 8 };

Your code will still report a duplicate - even though no values are repeated in that data!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 31, 2020 - 12:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sky33 wrote:
your solution doesn't seems to correct 
Eh? My solution worked perfectly. The result was:

 

That is exactly the case in:

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

element 0 with value 3 is duplicated at element 4. that is:

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

There is no question about this. Element 0 is most definitely duplicated by element 4. Also

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

element 1 is duplicated with element 6. Both my output showed this and yours:

$gcc -o main *.c
$main
element 0 with value 3 is duplicated at element 4
element 1 with value 5 is duplicated at element 6

 

Both those say EXACTLY what I just explained so in what sense is this "doesn't seem to correct".

 

I can only assume you are seeing something I'm not seeing or you don't understand ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
or you don't understand ?

I think he may be confusing the element number (index) with the value stored at that position in the array ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We're all programmers here. I thought it was kind of a basic fundamental that all programmers know what "element" and "value" mean ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

I have shown my best attempt

Well, that was you're first attempt.

 

Just trying to explain to you how to approach this so that you know how to do it yourself - rather than just giving a solution.

 

 

Again, don't jump straight to code - think about what to do first before trying to code how to do it.

 

okay so first trying to fix in flow chart 

 

 

Does it make any sense ? i will look help to fix flow chart first 

 

after last box test = 5, I do not understand what to do I am planning to use another loop. I have limited version that's why didn't make next loop  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
Does it make any sense ?

No.

 

You are still just using 3 as a magic number:

 

 

As explained, and as illustrated by clawson, you need to bre reading from the array.

 

Then looking again in the array to see if what you read is repeated.

 

 I have limited version that's why didn't make next loop  

Eh??

 

EDIT

 

fix quote

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 31, 2020 - 02:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

We're all programmers here. I thought it was kind of a basic fundamental that all programmers know what "element" and "value" mean ??

I understand array index and value 

array index are digits[0], digits[1], digits[2], digits[3], digits[4], digits[5], digits[6],

 

array values are 3, 5, 4, 2, 0, 9, 8

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

sky33 wrote:

your solution doesn't seems to correct 

Eh? My solution worked perfectly. The result was:

 

Maybe the problem is when you have more than 2 copies of a number, for example:

 

#include <stdio.h>
#define N 8

int digits[N] = { 3, 5, 4, 2, 3, 1, 5, 5 };

int main(void) {
    int test;
    for (int i = 0; i < N; i++) {
        test = digits[i];
        for (int j = i + 1; j < N; j++) {
            if (digits[j] == test) {
                printf("element %d with value %d is duplicated at element %d\n", i, test, j);
            }
        }
    }
    return 0;
}

 

Outputs:

element 0 with value 3 is duplicated at element 4
element 1 with value 5 is duplicated at element 6
element 1 with value 5 is duplicated at element 7
element 6 with value 5 is duplicated at element 7

 

In this situation some way to keep track of already tested duplicates would be needed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ha ha - that takes us back to #4 and a clear definition of what the problem actually is!

 

But (s)he really needs to get the basic problem sorted before adding more complications!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
I understand array index and value

So how can you possibly say that clawson's code in #8 is not giving the correct answer??!

 

Baffled!

 

array index are digits[0], digits[1], digits[2], ...

The indexes are just the highlighted numbers - 0, 1, 2, ... !

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Index = element number. It's the same thing. The array in my example has 7 elements numbered [0] to [6]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
You have to do multiple passes.

Actually - No - One pass should be sufficient, but you do need a container to store the number of occurrences of each integer.

 

If your set of numbers is only 0 .. 9 (or even some other low value) then that container becomes trivial.

 

I'd write some demonstration code but I've got other things to look at this evening.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have shown my best attempt to solve problem and trying best 

This is a terrible approach.   FIRST, in at least 3 or four COMPLETE SENTENCES, state what your trying to accomplish. 

Do not even try to think of the solution if you can't state what you are trying to do!

 

"I want to find duplicate number in array" I have tried on paper by making flow diagram 

This essentially says...Next to NOTHING useful are you seriously lacking all details?...try again!!!

 

"I want to buy a car but have a problem"  ...that is what you are stating...no useful details

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

I have shown my best attempt to solve problem and trying best 

This is a terrible approach.   FIRST, in at least 3 or four COMPLETE SENTENCES, state what your trying to accomplish. 

Do not even try to think of the solution if you can't state what you are trying to do!

 

 

Problem statement 

 

1. I want to identify that there is any duplicate ?

 

2. I want to count how many duplicates ?

 

3.  I want to list all the duplicated values ?

 

post #19 look at flow chart that is my attempt but Its not correct so I am looking help to fix flow chart first. It would appreciated  if any one can help to fix flow chart first

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is this a school assignment?

to me this is a big throwback in to the time I went to college. we had to do the exact same thing.

I have seen more exercises that I identify with having had a school.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 I want to identify that there is any duplicate ?

 

2. I want to count how many duplicates ?

 

3.  I want to list all the duplicated values ?

 

 

You still haven't explained what you want....What do you mean by identify?  What do you mean by duplicate?  Are 3 of the same item counted as 3 duplicates, or does each item have to be duplicated with one of its own clone?

 

So if you have 33  33  22  33  44  22   ....are you saying you have 5 duplicates, or maybe 2 duplicates?   Almost all of the values are duplicates of something, so here is your list:  (I want to list all the duplicated values ):    33  33  22  33  22   

 

...or do you simply want 22, 33 ??  

       

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
Problem statement 

 

1. I want to identify that there is any duplicate ?

 

2. I want to count how many duplicates ?

 

3.  I want to list all the duplicated values ?

 

No, that is not a problem statement!

 

That is just a copy & paste of my questions to you in #4 where I was asking you to properly define the requirement!

 

See also #22 and #23 - where further ambiguities were pointed out

 

And now #30 has highlighted some more things that you need to clarify.

 

These are really things that should come out in your analysis & design phase - long before you get into coding!

 

post #19 look at flow chart that is my attempt but Its not correct so I am looking help to fix flow chart first. It would appreciated  if any one can help to fix flow chart first

All of the posts after that are doing exactly that!

 

It's not about fixing a broken flowchart - it's about understanding the question first!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to go right back to basics, and think about how you would do this manually - with paper & pencil.

 

Forget all about flowcharts, computers, arrays - just think how you'd do this with a list of numbers on a piece of paper:

 

  1. First, you write down the list
  2. Then you consider the first entry in the list
  3. Then you look at each of the following entries, asking your self, "is this a duplicate?" 
    (whether you need to carry on past the 1st repetition depends on how you define the problem)
  4. Now you go back to the second entry in the list
  5. Again, you look at each of the following entries, asking your self, "is this a duplicate?" 
    (you don't need to look at the preceding entries, because you've already looked at them in the previous step)
  6. and so on - until you've done the whole list.

 

Now you can start thinking about how to implement that - a flowchart could be your next step ...

 

EDIT

 

Pencil & paper is also a very good way to test your flowchart & your code!

 

You go through each step of the code, or box of the flowchart - manually doing what it says, and noting what's happening.

 

Have you done that with your flowcharts - to see where they go wrong ?

 

Have you done that with clawson's code - to see how it gets it right ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 1, 2020 - 09:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

For example, as an implementation you might sort your values in numerical order by some scheme...then the sorted list makes finding "duplicates" easier...perhaps faster..perhaps slower overall (takes time to sort).  For example a bubble sort or linked list method

However, any of this comes AFTER you have clearly defined the problem & what you want to get out of it.  You must consider all cases so your needs are thoroughly described to cover all possibilities.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:

avrcandies wrote:

I have shown my best attempt to solve problem and trying best 

This is a terrible approach.   FIRST, in at least 3 or four COMPLETE SENTENCES, state what your trying to accomplish. 

Do not even try to think of the solution if you can't state what you are trying to do!

 

 

Problem statement 

 

1. I want to identify that there is any duplicate ?

 

2. I want to count how many duplicates ?

 

3.  I want to list all the duplicated values ?

 

post #19 look at flow chart that is my attempt but Its not correct so I am looking help to fix flow chart first. It would appreciated  if any one can help to fix flow chart first

OK so the way I would do this kind of depends on the point 

N.Winterbottom wrote:
If your set of numbers is only 0 .. 9 (or even some other low value) then that container becomes trivial.

If it is just the digits 0..9 then there's no problem having something like:

uint8_t count[10];

#define N 7

uint8_t digits[N] = { 3, 5, 4, 2, 3, 1, 5 };

int main(void) {
    for (int i = 0; i < N; i++) {
        count[digits[i]]++;
    }
    for (int i = 0; i < 10; i++) {
        if (count[i] > 1) {
            printf("value %d is duplicated %d times as items", i, count[i]);
            for (int j = 0; j < N; j++) {
                if (digits[j] == i) {
                    printf(" %d", j);
                }
            }
            printf("\n");
        }
    }
}

this outputs:

value 3 is duplicated 2 times as items 0 4
value 5 is duplicated 2 times as items 1 6

But if the data isn't just the digits 0..9 then you can't really keep a count[] array to cater for all positive integers that may occur so in this case I would suggest a linked list that grows dynamically for each duplicate found and something like:

typedef struct _count {
    uint8_t value;
    uint8_t count;
    struct _count * next;
} count_t;

(the types may need to change if you want to cater for values or counts above 255).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Way over the top for this question... but I recently had to evolve a mechanism to do a similar task, but with a much more complex data structure, looking to see whether duplicates existed, and if they did, whether other parts of the data were also duplicated - so  duplicates were acceptable, but only if every element in the structure was also a duplicate. It was a large structure which had to be parsed from a file of several megabytes of text... the data I had to parse was naturally not ordered.

 

This ended up as a multi-stage process.

 

1) for each line of text, parse it into a structure; walk a doubly linked list until I found a link with a higher value that the one I just parsed, and insert the new structure before that link. That gave me an ordered list, at the cost of having to walk an average of half the list for each new entry.

 

2) Now I have a nicely ordered list, start walking from the front and for each entry, see whether the previous link has the same numeric value; if so, copy a variable 'count' (initially zero) to the current link and increment it. Then zero the count for the previous link. Just one pass.

 

After this I have an ordered list, each entry of which has a count entry of either zero, or the number of matching entries. It's only a courtesy detail that the initial file is generated in China, and that because of contact issues, isn't necessarily even real ascii; it has holes and strange characters and all sorts of other rubbish that I have to handle when I parse it. And there's an awful lot of malloced memory to clean up afterwards :)

 

As I said, possibly a little overkill for the task at hand...

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Give a kid a bowl of Smarties and ask them to count the red ones, then the blue ones etc. the problem will get solved swiftly. Be it numbers or smarties, same problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
the problem will get solved swiftly. 

more likely, the problem will get eaten swiftly!

 

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

  What do you mean by duplicate?  Are 3 of the same item counted as 3 duplicates, or does each item have to be duplicated with one of its own clone?[/quote

 

3, 5, 4, 2, 3, 9, 8, 5

 

same item will counted as duplicated 

 

avrcandies wrote:

 

So if you have 33  33  22  33  44  22   ....are you saying you have 5 duplicates, or maybe 2 duplicates?   Almost all of the values are duplicates of something, so here is your list:  (I want to list all the duplicated values ):    33  33  22  33  22   

 

...or do you simply want 22, 33 ??  

 

 

I simply want 22, 33 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sky33 wrote:
I simply want 22, 33 

So, in other words, you want anything that occurs more than once?

 

Or anything that is non-unique.

 

This is a common problem with natural language specifications (ie, specifications in the languages people speak to each other).

 

When people speak to each other in natural languages, they tend to use words quite loosely; eg "duplicate" to mean "any repetition"  - rather than its precise meaning of "occurs exactly twice".

 

This is compounded when people are using a foreign language.

 

So the first step in defining the task is to get rid of all these ambiguities - sometimes called disambiguation.

 

So we are still waiting for you to give a complete & precise definition of your task.

 

If you can't give a complete & precise definition of your task, you cannot possibly write a 'C' program to solve it!

 

And it makes it very hard for people to help you - because we don't know what you are actually trying to achieve!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 1, 2020 - 01:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

So we are still waiting for you to give a complete & precise definition of your task.

In what way wasn't #28 complete? Dunno about anyone else but it seems pretty clear to me what he wants.

 

If I could be arsed I might have a crack at a linked list version.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See #31 !

 

EDIT

 

Also #38 & #39 - "duplicate" is ambiguous.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 1, 2020 - 01:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
"duplicate" is ambiguous.
Do we have different grasps of the English language then? Duplicate for me means something repeated, that is something that occurs more than once. Is there a question about that interpretation then??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I explained that in #39, and  avrcandies made a similar point in #30

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Well at least Google and I agree...

 

 

"or more" means it doesn't have to be just TWO but any number of two or more.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

duplicate is being used here as a noun not an adjective surely ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

So the first step in defining the task is to get rid of all these ambiguities - sometimes called disambiguation.

Although I may tend to agree that we've got a bit distracted with semantics, this thread is a clear demonstration of how this step can be a difficult one.

 

To the OP:  don't be discouraged.  The core point is still important:  You must describe the problem with sufficient clarity before you can begin to solve the problem.  Here, 'solve' means 'design', or in your case 'flowchart'.  Whereas 'write code' is the last step: implementation.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Apr 1, 2020 - 03:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
we've got a bit distracted with semantics

Indeed.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

To the OP:  don't be discouraged.  The core point is still important:  You must describe the problem with sufficient clarity before you can begin to solve the problem.  Here, 'solve' means 'design', or in your case 'flowchart'.  Whereas 'write code' is the last step: implementation.

 

I am really sorry if I couldn't  explain problem statement because english is not my first language. This is not homework assignment. I want to be a good programmer that's why I am making problem statement myself and trying to convert it into code. so I started to make flow chart and practicing with  paper and pencil    

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So go on - make you problem statement now.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i intended to write some C code to illustrate my #26; but because I think the OP would learn more by writing his own code, I thought perhaps not.

 

Instead I wondered how this might be solved in a higher level language. So here's a python implementation: Yep just a single line.

 

digits = [2,5,4,2,3,1,5]

duplicates = [digit for digit in set(digits) if digits.count(digit) > 1]

print (duplicates)

 

It works by creating a set of the numbers in the initial list These are unique with duplicates removed.

This set is iterated over and if the occurrence count of the number within the initial list is greater than 1 (i.e. is duplicated) that number is added to an output list named duplicates.

Finally the list of duplicates is printed.

 

nigel@E6420:~/Project/tmp /usr/bin/python3 /home/nigel/Project/tmp/duplicates.py
[2, 5]

 

Last Edited: Wed. Apr 1, 2020 - 08:46 PM

Pages