# duplicate number in array

#### skyr6546

Joined Mar 22, 2019
73
I want to merge the values of two arrays and if there is a duplicate number count them once

For example

x = { 1, 2, 3, 4 }
y = { 3, 4, 5, 6, 7 }

so result should be
z = { 1, 2, 3, 4, 5, 6, 7}

3 and 4 were duplicate values that I have merged

I want to write c code but I don't know logic What would be a simple algorithm for this

#### jpanhalt

Joined Jan 18, 2008
11,088
Will the arrays be ordered as you show? What's the maximum number of entries in each array?

#### skyr6546

Joined Mar 22, 2019
73
Will the arrays be ordered as you show?
Both of your questions are important. For now it doesn't matter what order they are, But the content of the duplicated value should not be repeated. This is my first priority.

What's the maximum number of entries in each array?
Now I assume the length of the array is 7.

#### peterdeco

Joined Oct 8, 2019
484
I don't know C but in basic I would set a flag on a duplicate bit to ignore it.

#### jpanhalt

Joined Jan 18, 2008
11,088
Nothing clever to offer right now. In Assembly, I would do a bubble sort like this: http://www.piclist.com/techref/microchip/sorts.htm

If any difference =0, set one value to zero. You then end up with a merged array and your values ordered. If "0" is allowed, then test low-end and discard all but one zero. (Edit: You may also need to test for zero in the initial arrays. Not certain of that, though.)

Testing for >, <, and/or = is dependent on the instruction set. However, in C, that dependence may not exist.

#### MrSoftware

Joined Oct 29, 2013
2,029
This is pretty ugly, but for homework understand what this does and simplify it. Note this could be written a ton simpler by relying on some library functions, especially if you use C++

MOD NOTE: Solution code removed. Please to NOT just provide solutions to homework!

Last edited by a moderator:

#### MrSoftware

Joined Oct 29, 2013
2,029
Here added a little C++, for homework you can clean it up.

MOD NOTE: Solution removed.

Last edited by a moderator:

#### BobaMosfet

Joined Jul 1, 2009
2,029
Title: Algorithms in C, 3rd Ed. [Parts 1-4, Fundamentals, Data Structures, Sorting, Searching]
Author: Robert Sedgewick
ISBN: 0-201-31452-5

#### WBahn

Joined Mar 31, 2012
27,474
I want to merge the values of two arrays and if there is a duplicate number count them once

For example

x = { 1, 2, 3, 4 }
y = { 3, 4, 5, 6, 7 }

so result should be
z = { 1, 2, 3, 4, 5, 6, 7}

3 and 4 were duplicate values that I have merged

I want to write c code but I don't know logic What would be a simple algorithm for this
I suspect that the point of this assignment is for you to figure out the algorithm, not to just have one given to you.

Can you break the problem into smaller and/or simpler pieces that are easier to solve?

For instance, what if you could count on the x and y arrays being sorted? Can you figure out a way to merge the two arrays so that the resulting array is sorted?

If you have a sorted array, can you figure out a way to remove duplicates from it (or create a new array that doesn't contain duplicates)?

Now let's consider if x and y aren't initially sorted? Can you figure out a way to sort them?

#### 402DF855

Joined Feb 9, 2013
271
Do NOT just provide solutions to homework problems -- hints, suggestions, and guidance. The student needs to do the bulk of the work, including the thinking involved.
If the array values are well bounded, I'd avoid sorting and use flags for inclusion. Assuming 0-255:

MOD NOTE: Solution removed.

#### panic mode

Joined Oct 10, 2011
2,303
i would say first treat each array separately - sort them on their own then remove duplicate elements.

then merge the two arrays into a third one by using loop and separate index for each array.
transfer element(s) from which ever array has smaller element at current index until end of one array is reached.
then add rest of elements from another array.