xboxrulz1405241485 0 Report post Posted December 3, 2008 Hey guys,Recently I had my test, but this question completely stumped me. I was wondering how you guys would approach this question:So basically I get two parallel arrays as such:userid_t[] 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005itemcode_t[] A , A , B , B , C , A , B , FSo basically, I have to go through the parallel arrays and see the frequency of them having two distinct users selecting the item code.So in that question, the array:itemcodes[] A, B, C, F <--- this part I got, just sort them and then drop in the ones that aren't duplicated.frequency[] 2, 3, 1, 1 <--- this is the part that I'm having trouble with.Many thanks,xboxrulz Share this post Link to post Share on other sites
turbopowerdmaxsteel 0 Report post Posted December 3, 2008 How about frequency being an array of List of integers? That way you can add the userids to it while checking that the same userid has not been added. Finally, just return the count of the list, that is your frequency. Share this post Link to post Share on other sites
docduke 0 Report post Posted December 3, 2008 My first question is: What tools can you use? My choice is Python.As an example, I took your items and made them into a list of characters:>>> x = ['A' , 'A' , 'B' , 'B' , 'C' , 'A' , 'B' , 'F']then I got rid of duplicates by converting the list to a set and back to a list:>>> y = list(set(x))Printing the result gives>>> y['A', 'C', 'B', 'F']You can then tell how many duplicates there were by counting and subtraction:>>> print len(x) - len(y)3>>>Extending this example, you can build lists for each item and the corresponding userids, and remove duplicates for each item.Hope this helps! Share this post Link to post Share on other sites
xboxrulz1405241485 0 Report post Posted December 3, 2008 I can't use any tools =(. It must be in pure C. As for turbomaxsteel's post ... I'm a bit lost at what you mean...Thanks,xboxrulz Share this post Link to post Share on other sites
turbopowerdmaxsteel 0 Report post Posted December 3, 2008 Here's how I would do it in C#. It'll be a bit difficult to do it in C, especially since you don't have classes. using System;using System.Collections.Generic;namespace Maxotek{ static class Program { static void Main() { int[] userid_t = new int[] { 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005 }; char[] itemcode_t = new char[] { 'A', 'A', 'B', 'B', 'C', 'A', 'B', 'F' }; // Remove Duplicate Itemcodes List<char> itemcodes = new List<char>(); for (int i = 0; i < itemcode_t.Length; i++) if (!itemcodes.Contains(itemcode_t[i])) itemcodes.Add(itemcode_t[i]); // Sort Itemcodes itemcodes.Sort(); List<int>[] frequency = new List<int>[itemcodes.Count]; for (int i = 0; i < userid_t.Length; i++) { int j = 0; for (; j < itemcodes.Count; j++) { if (itemcodes[j] == itemcode_t[i]) break; } if (frequency[j] == null) { frequency[j] = new List<int>(); frequency[j].Add(userid_t[i]); } else if (!frequency[j].Contains(userid_t[i])) frequency[j].Add(userid_t[i]); } // Display the result for (int i = 0; i < itemcodes.Count; i++) { Console.WriteLine(itemcodes[i] + "-" + frequency[i].Count); } Console.Read(); } }} With List<int>[] frequency = new List<int>[itemcodes.Count];, I have declared an array of list of integers. It stores the userids that selected the itemcode at the corresponding index in the sorted itemcodes list. Before adding the userid to the list, we check if it has been already added and skip the step if it is. At the end we can obtain the distinct users by retrieving the count property of the each of the lists. Share this post Link to post Share on other sites
xboxrulz1405241485 0 Report post Posted December 4, 2008 It looks so much easier in C#, and probably similarly easy on Java (my preferred language)... I'll see if I can translate that back to C since that's what I'm learning this semester. I'm actually hating C >.>".I'm learning C++ next semester so hopefully, it's gonna make it easier for someone who just loves programming with Java.xboxrulz Share this post Link to post Share on other sites