添加重复值 [英] Add value of duplicates

查看:79
本文介绍了添加重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

int [] array1 = {1,2,3,4,2,4,5}

int [] array2 = {7,8 ,9,10,4.4,2,3}



i想要输出如下



int [] array1 = {1,2,3,4,5}

int [] array2 = {7,12.4,12,10,4.4,3}



添加重复项 - 比如2重复,2在array2中有8,4.4值

与array1中重复4相同,它在array2中的值为10和2

现在我希望2的显示值是12.4,4的值是12.



我尝试了什么:



尝试下面的解决方案

i have
int[] array1 = {1,2,3,4,2,4,5}
int[] array2 = {7,8,9,10,4.4,2,3}

i want output like below

int[] array1 = {1,2,3,4,5}
int[] array2 = {7,12.4,12,10,4.4,3}

addition of duplicates - like 2 is repeated and 2 has 8,4.4 values in array2
same as 4 is repeated in array1 and it has values 10 and 2 in array2
now i want show value for 2 is 12.4 and value for 4 is 12.

What I have tried:

tried below solution

class Program
	{
		public static void Main(string[] args)
		{
			int [] sArray = {10,20,30,40,40,50,60};
			var sList = new ArrayList();
			
			for (int i = 0; i < sArray.Length; i++) {
			    if (sList.Contains(sArray[i]) == false) {
			        sList.Add(sArray[i]);
			    }
			}
			
			var sNew = sList.ToArray();
			
			for (int i = 0; i < sNew.Length; i++) {
			    Console.Write(sNew[i]);
			}
			Console.ReadLine();
		}
	}

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是你想的很难!

我会给你一个提示:你的sList包含sArray中重复的所有数字 - 所以你需要使用那些信息来查找第二个数组中的所有数字在原始数组中使用匹配的索引 - 你不是这样做的,你打印的是重复的数字,而不是与第二个数组有关。

就个人而言,我是这样的它完全不同 - 我创建了一个类(或结构),其中包含值,遇到的次数,以及第二个数组的总值,然后将其存储在一个唯一值数组中,这样我只需要通过第一个数组一次。然后,我将从该数组中打印出计数大于1的数组。但是对你来说这可能有点先进,所以尝试用第二个循环来累加每个重复的值。



如果遇到特定问题,那么请询问,我们会尽力提供帮助。但是我们不会为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
I'll give you a hint: Your sList contains all the numbers that are duplicated in sArray - so you need to use that information to find all the numbers in the second array with matching indexes in the original array - you aren't doing that, you are printing the numbers that are duplicated, rather than anything to do with the "second array".
Personally, I'd so it very differently - I'd create a class (or struct) which contained the value, the number of times it was encountered, and the total value from the second array, then store that in an array of unique values, so that I only need to pass through the first array once. I'd then print from that array the ones with a count greater than one. But that may be a little advanced for you, so try it with a second loop to add up the values for each repeat.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


我们不做你的HomeWork。

HomeWork不会测试你的技能在乞求其他人们做你的工作,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



因为我理解这个问题:

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

as I understand the problem:
Each time you find a duplicate in array1 at position x and y,
    you add array2[y] to array2[x]
    remove array2[y] and array1[y]



您只需要将其翻译成代码。


You just have to translate this to code.


这篇关于添加重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆