fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4. #include <algorithm>
  5. #include <map>
  6. //MITライセンス です。
  7.  
  8. typedef std::vector<std::int64_t> DType;
  9.  
  10. template<class Container>
  11. typename Container::value_type MakeHoge3(const Container& D) {
  12. std::map < typename Container::value_type, std::uintmax_t> M;
  13.  
  14. for (auto& o : D) {
  15. M[o]++;
  16. }
  17. for (auto& o : M) {
  18. if (o.second == 1) {
  19. return o.first;
  20. }
  21. }
  22. return { -1 };
  23. }
  24. /** /
  25.  
  26. typedef std::map<std::int64_t, std::uint64_t> MType;
  27.  
  28. std::int64_t MakeHoge2(const DType& D) {
  29. MType M;
  30.  
  31. for (auto& o : D) {
  32. M[o]++;
  33. }
  34. for (auto& o : M) {
  35. if (o.second == 1) {
  36. return o.first;
  37. }
  38. }
  39. return -1;
  40.  
  41. }
  42. /**/
  43.  
  44. /** /
  45. DType::value_type MakeHoge(DType D) {
  46.  
  47. std::sort(D.begin(), D.end());
  48. std::size_t i = 0;
  49. for (i = 1; i < D.size() - 1; i++) {
  50. if (D[i - 1] != D[i]) {
  51. if (D[i] == D[i + 1])continue;
  52.  
  53. return D[i];
  54. }
  55. }
  56. if (D[i] != D[i - 1])return D[i];
  57. return -1;
  58. }
  59. /**/
  60. int main() {
  61. DType D = { 1,1,1,1,2,2,2,3,3,4 };
  62. std::cout << MakeHoge3(D) << std::endl;
  63. D = DType{ 1,2,3,4,5,5,4,3,2,1 };
  64. std::cout << MakeHoge3(D) << std::endl;
  65. D = DType{ 3,1,4,1,5,9,2,6,5,3,5 };
  66. std::cout << MakeHoge3(D) << std::endl;
  67. return 0;
  68. }
  69.  
  70.  
Success #stdin #stdout 0s 4480KB
stdin
Standard input is empty
stdout
4
-1
2