算法作业

Report

Problem

For n elements, find all elements from a list provided that shows more than n/4 times.

Input

Java types (String, numbers and char) as a list

Output

Elements found or not

Analysis

There was a group discussion about how represent elements the question provided. How to build a collection to store all different factors and visit easily? HashMap is one solution to that problem.

Solution

To build a list {"Student",‘A’,5,'A','A',5,5,"Student",'A',5,"Human",100}.

There are three types in the list, String, char and number. Put all the elements into a HashMap, and give them keys individually. Then, build a iterator for traversing all the HashMap values. When it found a match one, a counter will plus one.

After the iterator finished, take the result of the counter, compare it with n/4 times, if more than it, then the element is the right one which will be collected into a HashSet (because of the property of a set: when put the same elements into a set, it will not exist more than one time).

 Following this, there is an outside loop to go through all the elements of the list from one to another. After the outside loop finished, print the HashSet which contains all correct and distinct answers.

Result

n = 12

n / 4 = 3

Result is {5, A}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值