duplicate number in array

Thread Starter

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
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,015
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!


1582381702168.png
 
Last edited by a moderator:

MrSoftware

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

MOD NOTE: Solution removed.


1582382733784.png
 
Last edited by a moderator:

BobaMosfet

Joined Jul 1, 2009
1,776
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
26,398
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
1,882
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.
 
Top