Kth Largest Number

Problem

Hyeonsu has N cards with natural numbers between 1 and 100. There can be multiple cards of the same number. Hyeon-soo draws 3 of them and tries to record the sum of the numbers on each card. Record all three possible draws. Write a program that prints the Kth largest number among recorded values.
If the number from the largest number is 25 25 23 23 22 20 19… and the value of K is 3, then the Kth largest value is 22.

input

Natural numbers N(3<=N<=100) and K(1<=K<=50) are input in the first line, and N card values are input in the next line.

10 3
13 15 34 23 45 65 33 11 26 42

output

Print the Kth number on the first line. The Kth number must exist.

143

Solution

using Set()

function solution(n, k, card) {
	let answer;
	let tmp = new Set();

	for (let i = 0; i < n; i++) {
		for (let j = i + 1; j < n; j++) {
			for (let k = j + 1; k < n; k++) {
				tmp.add(card[i] + card[j] + card[k]);
			}
		}
	}

	let a = Array.from(tmp).sort((a, b) => b - a);
	answer = a[k - 1];

	return answer;
}

not using Set()

function solution(k, card) {
	let answer;
	const result = [];

	for (let i = 0; i < card.length - 2; i++) {
		for (let j = i + 1; j < card.length - 1; j++) {
			for (let k = j + 1; k < card.length; k++) {
				let num = card[i] + card[j] + card[k];
				if (!result.includes(num)) result.push(num);
			}
		}
	}

	result.sort((a, b) => b - a);
	answer = result[k - 1];

	return answer;
}