Java interviewers like to ask how to solve List loop repeated data

Java interviewers like to ask how to solve List loop repeated data

Preface

Sometimes it is necessary to deduplicate List data at work. During interviews, interviewers also like to ask how to deduplicate data. In fact, this article is more suitable for novices to learn
The interviewer asked how to remove duplicates from List data because this is a very basic question that involves basic data structure and algorithm knowledge in Java or other programming languages. At the same time, this is also a very practical issue, because in actual development, we often need to deduplicate duplicate data.

By asking this question, the interviewer can test the interviewer's understanding and mastery of data structures such as List and Set, as well as his proficiency in basic programming concepts such as loops and conditional statements. In addition, the interviewer can also ask further questions to gain a deeper understanding of the interviewer's thinking process and problem-solving ability.

In short, although this question is simple, it is a good starting point to help the interviewer understand the interviewer's basic knowledge and practical application ability.

1. List deduplication code example

In Java, you can use the following methods to deduplicate List:

  1. Use Set: Convert List into Set collection. Since the elements of Set collection are unique, duplicate elements can be removed. Then convert the Set back to List.

Sample code:

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 4, 5));
Set<Integer> set = new HashSet<>(list);
list.clear();
list.addAll(set);
System.out.println(list);

Output result:

[1, 2, 3, 4, 5]
  1. Use loop traversal: traverse each element in the List, and if the element does not appear before, add it to a new List.

Sample code:

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 4, 5));
List<Integer> newList = new ArrayList<>();
for (Integer i : list) {
    if (!newList.contains(i)) {
        newList.add(i);
    }
}
System.out.println(newList);

Output result:

[1, 2, 3, 4, 5]
  1. Using Java 8 Stream API: Use the distinct() method of Stream to remove duplicate elements in the List.

Sample code:

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 4, 5));
List<Integer> newList = list.stream().distinct().collect(Collectors.toList());
System.out.println(newList);

Output result:

[1, 2, 3, 4, 5]

It should be noted that the above three methods will change the order of the original List. If you need to preserve the order of the original List, you can use LinkedHashSet or a custom function to achieve this.

2. Interview answers

When the interviewer asked how to remove duplicates from a list, I felt that the answer could be as follows, which can be used as a reference. I think there are three simple ways to deal with it
1. Use Set: Use the distinct() method of Stream to remove duplicate elements in the List. 3. Use Java 8 Stream API: Traverse each element in the List, if the element If it does not appear before, it is added to the new List.
2. Use loop traversal: Convert List into Set collection. Since the elements of Set collection are unique, duplicate elements can be removed. Then convert the Set back to List. How to ensure the original order can be achieved using LinkedHashSet or a custom function

If the interviewer asks, is there any other way to deduplicate? The answer is definitely yes. You can use map. Map keys are not allowed to be repeated. In addition, you can use the set data type of redis to deduplicate.

Some interviewers are very nonsensical and may ask, "Well, I have deduplicated it. I still want to keep the data that was not deduplicated before. How do I organize it? I also need to count the number of duplicate data. If you want to tell me, you did this on purpose." If you make things difficult for me, it's okay if I pass the interview, but if you deliberately make things difficult for me, I'll give you a shit. Once during an interview, I was asked what JavaConfig was, but I had already mentioned what Javaconfig was before, and asked me again, I directly asked if you were a programmer, and they said no. They asked me the same interview questions, and I was very confused. I answered the same as the interview questions, and it seemed that I was leaning on my back. If I said something different, it was all based on my own understanding. To express myself, it seems that I am not telling the truth. This is what the interviewer said about me. He said that I was wrong. Asking the interview questions bit by bit was really confusing. So during the interview, if the other person is a programmer, he would answer according to his own understanding. He would not be able to express his understanding. Just answer according to the interview questions. If it is HR or other, then find someone who can recite eight-part essay with one character or less. I said this question is digressing, but I am very angry. As a programmer, I am here to apply for a job. I didn’t mean to make things difficult for you. I just looked at the interview questions and asked me. If I don’t know anything about the technology, it’s useless to interview me. I can’t understand the difficult and in-depth ones. If you tell me something simple and easy to understand, you can tell me how to record it in txt. Why is it different? He must have said it wrong and it really lowered my outlook. Let’s go back to the list question. This question is very simple. It just tells you how to do the process. It’s very simple. I won’t go into details. Just to remind everyone, some interviewers are not recruiting people at all. I have met many of them. It's all for HR to improve performance. When you encounter this question, you should answer it well. What if you pass it? I also encountered this interview question four or five times when I first started working. Today, I suddenly remembered it and asked a few words.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_49841284/article/details/134085723