How does our brain work to find repeat number

WBahn

Joined Mar 31, 2012
30,088
[1,2,1,2,1]

I know that I have to compare one element with the other element.

I wondered what should happen if a number repeats three times in list
The problem statement needs to make it clear what should happen, which is why people have been asking these kinds of questions because the person solving the problem isn't the person that determines this. There are all kinds of problems that could be posed involving identifying repeated numbers in a list. If I have a list that should not have any repeated values, a valid solution may do nothing more than return True as soon as the first occurrence of a repeated value is found and False only if no repeats are found. But another problem may require that a list of all values that occur more than once be returned. Yet another might want a count of how many different values are repeated. Many other possibilities exist.

So you don't need to wonder what should happen, you need to specify what you want to have happen (since you are the one defining the problem).
 

jpanhalt

Joined Jan 18, 2008
11,087
[1,2,1,2,1]

I know that I have to compare one element with the other element.
Here's a thought experiment:
1) Count to five. Write it down.
2) Count to five again. Write it down.
Are the 2 numbers you have written down the same? Did you have to compare them or do you know they are the same based on how they were calculated? (Excluding the real life consideration that someone may not be able to count to 5 accurately.)

Thinking ahead, "compares" with MCU's usually involve subtraction (or its cousin XOR'ing) and testing for zero. That is a fair number of steps.

I wondered what should happen if a number repeats three times in list
That gets to my earlier posit that you need to know the question before you can answer it. That is up to you. Define your question. Define how big the data set will be. Define the range of the data set.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
The problem statement needs to make it clear what should happen, which is why people have been asking these kinds of questions because the person solving the problem isn't the person that determines this.
Let's assume list = [ 4, 2, 3, 2, 1, 7, 19, 245, 4, 6, 1, 3, 3, 4, 245]

  1. Are there any duplicate in list?
  2. What are the duplicate in list
  3. How many time duplicate occur in list

A normal person will compare the first element with other elements in the list to find a duplicate number. I did the same

4-2 Not duplicate
4-3 Not duplicate
4-2 Not duplicate
4-1 Not duplicate
4-7 Not duplicate
4-19 Not duplicate
4-245 Not duplicate
4-4 Duplicate
4-6 Not duplicate
4-1 Not duplicate
4-3 Not duplicate
4-3 Not duplicate
4-4 Duplicate
4-245 Not duplicate

2-3 Not duplicate
2-2 Duplicate
2-1 Not duplicate
2-7 Not duplicate
2-19 Not duplicate
2-245 Not duplicate
2-4 Not duplicate
2-6 Not duplicate
2-1 Not duplicate
2-3 Not duplicate
2-3 Not duplicate
2-4 Not duplicate
2-245 Not duplicate

3-2 Not duplicate
3-1 Not duplicate
3-7 Not duplicate
3-19 Not duplicate
3-245 Not duplicate
3-4 Not duplicate
3-6 Not duplicate
3-1 Not duplicate
3-3 Duplicate
3-3 Duplicate
3-4 Not duplicate
3-245 Not duplicate

2-1 Not duplicate
2-7 Not duplicate
2-19 Not duplicate
2-245 Not duplicate
2-4 Not duplicate
2-6 Not duplicate
2-1 Not duplicate
2-3 Not duplicate
2-3 Not duplicate
2-4 Not duplicate
2-245 Not duplicate

1-7 Not duplicate
1-19 Not duplicate
1-245 Not duplicate
1-4 Not duplicate
1-6 Not duplicate
1-1 Duplicate
1-3 Not duplicate
1-3 Not duplicate
1-4 Not duplicate
1-245 Not duplicate

7-19 Not duplicate
7-245 Not duplicate
7-4 Not duplicate
7-6 Not duplicate
7-1 Not duplicate
7-3 Not duplicate
7-3 Not duplicate
7-4 Not duplicate
7-245 Not duplicate

19-245 Not duplicate
19-4 Not duplicate
19-6 Not duplicate
19-1 Not duplicate
19-3 Not duplicate
19-3 Not duplicate
19-4 Not duplicate
19-245 Not duplicate

245-4 Not duplicate
245-6 Not duplicate
245-1 Not duplicate
245-3 Not duplicate
245-3 Not duplicate
245-4 Not duplicate
245-245 Duplicate

4-6 Not duplicate
4-1 Not duplicate
4-3 Not duplicate
4-3 Not duplicate
4-4 Duplicate
4-245 Not duplicate

6-1 Not duplicate
6-3 Not duplicate
6-3 Not duplicate
6-4 Not duplicate
6-245 Not duplicate

1-3 Not duplicate
1-3 Not duplicate
1-4 Not duplicate
1-245 Not duplicate

3-3 Duplicate
3-4 Not duplicate
3-245 Not duplicate

3-4 Not duplicate
3-245 Not duplicate

4-245 Not duplicate

Answer
  1. Yes there are the duplicate in list
  2. 4, 2, 3, 1, 246 are duplicate numbers in list

  1. 4 occur 3 time's
2 occur 2 time's
3 occur 3 time's
1 occur 2 time's
245 occur 2 time's

In this way a person can find out if there is a duplicate number or not in list and how many times the number is being duplicated in a list.

In my mind I know how to do it but the most difficult task is to convert it into a flowchart

dl324 has given a good flowchart, I am trying to test this by placing a value in this flowchart.
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
Let's assume list = [ 4, 2, 3, 2, 1, 7, 19, 245, 4, 6, 1, 3, 3, 4, 245]

  1. Are there any duplicate in list?
  2. What are the duplicate in list
  3. How many time duplicate occur in list

A normal person will compare the first element with other elements in the list to find a duplicate number. I did the same.
Are you still trying to duplicate a human process or develop a machine algorithm/program? If it's the former, what you think you did may not be what you brain actually did nor what anyone else's brain would do. Simply put, you wrote the problem. Therefore you cannot act as a non-biased solver.

In my mind I know how to do it but the most difficult task is to convert it into a flowchart

dl324 has given a good flowchart, I am trying to test this by placing a value in this flowchart.
That is a fairly generic flow chart. It certainly applies to several of the solutions in the other thread. This step is the challenge:

1596010478408.png

What ways can you propose for doing that?
 

WBahn

Joined Mar 31, 2012
30,088
Let's assume list = [ 4, 2, 3, 2, 1, 7, 19, 245, 4, 6, 1, 3, 3, 4, 245]

  1. Are there any duplicate in list?
  2. What are the duplicate in list
  3. How many time duplicate occur in list

A normal person will compare the first element with other elements in the list to find a duplicate number. I did the same

4-2 Not duplicate
4-3 Not duplicate
4-2 Not duplicate
4-1 Not duplicate
4-7 Not duplicate
4-19 Not duplicate
4-245 Not duplicate
4-4 Duplicate
4-6 Not duplicate
4-1 Not duplicate
4-3 Not duplicate
4-3 Not duplicate
4-4 Duplicate
4-245 Not duplicate

2-3 Not duplicate
2-2 Duplicate
2-1 Not duplicate
2-7 Not duplicate
2-19 Not duplicate
2-245 Not duplicate
2-4 Not duplicate
2-6 Not duplicate
2-1 Not duplicate
2-3 Not duplicate
2-3 Not duplicate
2-4 Not duplicate
2-245 Not duplicate

3-2 Not duplicate
3-1 Not duplicate
3-7 Not duplicate
3-19 Not duplicate
3-245 Not duplicate
3-4 Not duplicate
3-6 Not duplicate
3-1 Not duplicate
3-3 Duplicate
3-3 Duplicate
3-4 Not duplicate
3-245 Not duplicate

2-1 Not duplicate
2-7 Not duplicate
2-19 Not duplicate
2-245 Not duplicate
2-4 Not duplicate
2-6 Not duplicate
2-1 Not duplicate
2-3 Not duplicate
2-3 Not duplicate
2-4 Not duplicate
2-245 Not duplicate

1-7 Not duplicate
1-19 Not duplicate
1-245 Not duplicate
1-4 Not duplicate
1-6 Not duplicate
1-1 Duplicate
1-3 Not duplicate
1-3 Not duplicate
1-4 Not duplicate
1-245 Not duplicate

7-19 Not duplicate
7-245 Not duplicate
7-4 Not duplicate
7-6 Not duplicate
7-1 Not duplicate
7-3 Not duplicate
7-3 Not duplicate
7-4 Not duplicate
7-245 Not duplicate

19-245 Not duplicate
19-4 Not duplicate
19-6 Not duplicate
19-1 Not duplicate
19-3 Not duplicate
19-3 Not duplicate
19-4 Not duplicate
19-245 Not duplicate

245-4 Not duplicate
245-6 Not duplicate
245-1 Not duplicate
245-3 Not duplicate
245-3 Not duplicate
245-4 Not duplicate
245-245 Duplicate

4-6 Not duplicate
4-1 Not duplicate
4-3 Not duplicate
4-3 Not duplicate
4-4 Duplicate
4-245 Not duplicate

6-1 Not duplicate
6-3 Not duplicate
6-3 Not duplicate
6-4 Not duplicate
6-245 Not duplicate

1-3 Not duplicate
1-3 Not duplicate
1-4 Not duplicate
1-245 Not duplicate

3-3 Duplicate
3-4 Not duplicate
3-245 Not duplicate

3-4 Not duplicate
3-245 Not duplicate

4-245 Not duplicate

Answer
  1. Yes there are the duplicate in list
  2. 4, 2, 3, 1, 246 are duplicate numbers in list

  1. 4 occur 3 time's
2 occur 2 time's
3 occur 3 time's
1 occur 2 time's
245 occur 2 time's

In this way a person can find out if there is a duplicate number or not in list and how many times the number is being duplicated in a list.

In my mind I know how to do it but the most difficult task is to convert it into a flowchart

dl324 has given a good flowchart, I am trying to test this by placing a value in this flowchart.
So let's examine what you had to do in order to come up with this procedure for this specific example. What we end up with will reflect this algorithm, which is not the most efficient by quite a bit. But that's fine since you are exploring how to go from "this is an approach I can think of for how I would do it" to "this is how a computer could implement this approach".

Think about what you do in the first group of comparisons that you have:

In Group #1 you are comparing the Item #1 in the list to other items in the list starting with Item #2 and continuing through Item #K. (Where K is the number of items in the list)

Now, in these same terms, what are you doing in the next couple of groups?

In Group #2 you are comparing the Item #2 in the list to other items in the list starting with Item #3 and continuing through Item #K. (Where K is the number of items in the list)
In Group #3 you are comparing the Item #3 in the list to other items in the list starting with Item #4 and continuing through Item #K. (Where K is the number of items in the list)

Do you see the pattern here?

In Group #n you are comparing the Item #n in the list to other items in the list starting with Item #(n+1) and continuing through Item #K. (Where K is the number of items in the list)

What is the number of the last group?

We only do a single comparison, so we need it to read

In Group #(K-1) you are comparing the Item #(K-1) in the list to other items in the list starting with Item #K and continuing through Item #K. (Where K is the number of items in the list)

We can now write our algorithm that walks through the list comparing every item to every other item in very simple terms:

Code:
LET n := 1 // Start with item n=1 in a list of K items
WHILE n < K: Continue until we have used all but the last item in the list as the value we compare the rest of the list to
   LET k := n+1 // Start our comparisons with the next item in the list
   WHILE k <= K: // Continue until we have processed the last element in the rest of the list
      IF item[n] = item[k]:
         // Do whatever needs to be done if the items are equal
      LET k := k+1 // Continue with the next item in the current group of comparisons
   LET n := n+1 // Go to the next group of comparisons
Now you can focus on what you need to do each time you find two values that match.

If we JUST want to know if there exists at least one repeated value, then this is really easy and we could do the following:

Code:
LET repeatsInList := False // Start out with the assumption that there are no repeats
LET n := 1 // Start with item n=1 in a list of K items
WHILE n < K: Continue until we have used all but the last item in the list as the value we compare the rest of the list to
   LET k := n+1 // Start our comparisons with the next item in the list
   WHILE k <= K: // Continue until we have processed the last element in the rest of the list
      IF item[n] = item[k]:
         LET repeatsInList := True // Record the fact that we found a repeat
      LET k := k+1 // Continue with the next item in the current group of comparisons
   LET n := n+1 // Go to the next group of comparisons
But as soon as you want to know more detailed information, such as a record of all the values that are repeated and how many times they are repeated, things get more complicated.

Think about what you did in your process above to get your final results. I'll bet that after doing all of the comparisons and saying whether they were a duplicate or not for each one, you then had to go back through that entire list and identify the distinct values that were repeated and also count how many times they were repeated. But once you make a comparison and declare whether it is a duplicate or not, you need to do something RIGHT THEN. You can't come back because the result of the comparison is lost as soon as you move on to the next item.

In fact, you did quite a bit more than just go back through the list of comparison results and count things. In fact, I doubt you actually went back through the list of comparison results at all, but rather YOU went through the list and counted occurrences yourself.

Why did you say that 3 occurred 3 times (based on the results of your comparisons, not on you as a human scanning the list and counting them)?

Did you really just count the number of times that you had the statement:

3-3 Duplicate

and because it was there three times you said that 3 occurred 3 times?

If so, then why did you say that the other repeats occurred 2 times given that, in each case, there was only one such comparison success?

How many comparison successes would there have been for a number that appeared 4 times? 5 times?

Consider the list [2,2,2,2,2,2,2]

Every comparison, all 21 of them, are going to succeed. So how do I get from that to the statement that the number 2 occurred 7 times?

At this point it is often a good idea to take a step back and ask if there is an easier way.

What if the list was presented in sorted order:

[ 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 6, 7, 19, 245, 245]

Can you think of a simple algorithm that you could use to go through this list in a single pass and produce the following results:

1 occurs 2 times
2 occurs 2 times
3 occurs 3 times
4 occurs 3 times
245 occurs 2 times

If you can, then it makes sense to break your problem down into two smaller and independent problems:

Step #1: Given a list of N integers in random order, sort the list in ascending order.
Step #2: Given a list of N integers in ascending order, process the list to produce a list of number of occurrences for each repeated number.
 

WBahn

Joined Mar 31, 2012
30,088
@WBahn

I'm straggling in my first pass. I do not understand for diamand box of check duplicate, what to write in it, what to do in it
So a diamond box involves asking a yes-no question and then following the corresponding arrow out of it.

Since flow charts are meant to be understood by humans, the way that questions are phrased can be pretty sloppy as long as the intent is evident. But the more precise you make this question, the easier it will be to convert the flowchart into a program successfully.

So in this case you could, at one end of the sloppiness scale, say something like "Are the two numbers equal?" As long as the gist of how the process works is evident, most readers would be able to figure out which two numbers are being compared. But that's really iffy if someone that doesn't have the basic idea already in mind sits down and tries to follow it.

We know we are comparing two numbers in a list and so we need a way to identify, specifically, which two numbers are being compared. Your picture actually gives us the means to do this because it has a list of indices written above the numbers. So if I were to say, "Is the 4th number in the list equal to the 8th number in the list?" You would look up and see that the 4th number is 2 while the 8th number 245 and so the answer of whether they are equal is "No".

To indicate that we are referring to number 4 in a list of numbers, we give the list a name (often times just "list") and put the index in square brackets after the name of the list (we might write the index as a subscript in math, but that's not an option with most programming languages so we use the square brackets because it's something we can type).

So we might ask, "Is list[4] equal to list[8]?"

But we need a way of comparing different pairs of numbers each time we pass through this diamond, so we use variables. Common variables in small loops are 'i', 'j', and 'k', but you can use any name you want.

So we might ask, "Is list[i] equal to list[j]?"

When we put our question inside a diamond symbol, we KNOW we are asking a question and we know it is a yes-no question, hence we don't need the "Is" at the beginning and we don't need the "?" at the end, so we can just put "list[i] = list[j]".

To make this even more compact (and easier to convert to a program) we usually use mathematical operator symbols where possible, so we would just put in the diamond, "list[i] = list[j]".
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
This is not part of any of the algorithms I referred to:
1596019293026.png

Duplicates are not treated the same as non-duplicates.

Look at the algortithm I & Mr.Chips posted. How are duplicates determined in those specific examples? There are other ways. What are your thoughts?
 

jpanhalt

Joined Jan 18, 2008
11,087
What about contributing your thoughts to the questions asked in posts #26 and #30. You have been given examples that worked. Try to understand them. Then add your insights.

You now have posted two different flowcharts that lack critical elements needed to continue. Jumping around from one "I don't know the next step" to another will get nowhere. Maybe you don't know exactly the next step. At least give it some thought and present your ideas before switching to another, equally incomplete "flowchart."

I am not here to learn how to do it. I gave you a link to my solution and also described it in this thread. I am trying to help you to solve the question in your own way. That requires your focus and input.
 

Deleted member 115935

Joined Dec 31, 1969
0
Can I bring you back to the original question, which was "how does the brain work ?".. doing a certain thing.

I don't see any way that can ever be answered, at best we have a model of how we think it works,
as such, how can this be a question for the programming language section, may be for the off topic section.
 

jpanhalt

Joined Jan 18, 2008
11,087
From post #1, "I want to know that [how the brain works] I will try to make a program near it."

@andrewmm
I agree, as details of how brains (human and monkey) solve on such problems seem to be unknown. However, I have not followed any research on it.

At a simpler level, namely automation, those attempts I am conversant with that tried to duplicate what humans did (e.g., clinical laboratory testing) failed as they were to complex to keep working.

Right now, the TS seems to be focused on drawing a flow chart for doing something without himself having a plan in mind for doing it.
 

dl324

Joined Mar 30, 2015
16,944
I have started to make a flow chart but I do not understand how to make it further
We don't usually put code in a flow chart because a flow chart is an algorithm for solving the problem, not an implementation of that algorithm.

In Perl, looping through the numbers and determining the number of occurrences can be done with one statement/command.

If N is the index for an array of integers (called LIST) containing your list of numbers, you just step through the array and test each number against numbers you've already seen.

If you use another array to keep track of the numbers you've seen, you can use the current number as the index into the numbersSeen array:

numbersSeen[numberList[N]]++

You're using 1 as the index for the first array element. This will cause problems if valid numbers includes 0. I'm also assuming that negative numbers and non-integers are disallowed. If the latter are allowed, a different implementation will be needed.

This algorithm won't be very efficient if most of the elements in numbersSeen aren't used.
 
Last edited:
Top